I called a getElements
method which returns Iterable
.
I did this:
List elements = (List
Yes, List
extends Iterable
, but that doesn't mean that you can cast from any Iterable
to List
- only when the value actually refers to an instance of a type of List
. It's entirely possible to implement Iterable
without implementing the rest of the List
interface... in that case, what would you expect to happen?
To put it in simpler terms, let's change Iterable
to Object
and List
to String
. String
extends Object
, so you can try to cast from Object
to String
... but the cast will only succeed at execution time if the reference actually refers to a String
(or is null).