I was wondering if its possible to directly cast objects within a foreach loop.
We have the following two classes were one extends the other:
class Book {};
class ExtendedBook extends Book {};
Now we have an array of Books which I want to loop through, because its a ExtendedBook search I’m sure that all Books are actually Extended Books. Is there a way to directly cast them?
Book [] books = bookSearch.getBooks("extendedBooks");
for (Book book: books){
ExtendedBook eBook = (ExtendedBook) book;
....
}
This involves two steps. First looping through the books and at the second step casting them. Can one do it in one step?
What doesn’t work:
// Directly assign it to a different type
for (ExtendedBook book : books){}
// Directly casting the array
ExtendedBooks [] eBooks = (ExtendedBooks []) books;
// Same goes for trying both in one step
for (ExtendedBook book : (ExtendedBook []) books){}
I know its not a real pain, but keeping the loop shorter would be nice and maybe more readable as you save a dummy variable, which is just used for casting instead of the actual action.
I’m reasonably sure you cannot cast in the loop as you would like.