Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface casting for `Iterable`

If I have a class called Cue that implements Tickable. Later, I have an observable list like so: ObservableList<Cue> oList. oList implements Iterable<Cue> (that's just how ObservableList is declared).

I have a function that takes Iterable<Tickable>. How do I get it to accept ObservableList<Cue> Cue is a Tickable and ObservableList is a Iterable. For some reason, I can't caste between them or upcast automatically. Is there any way to do this?

like image 239
sinθ Avatar asked May 22 '26 16:05

sinθ


1 Answers

You cannot cast from an Iterable<Cue> to an Iterable<Tickable>, because even though a Cue is a Tickable, an Iterable<Cue> is not an Iterable<Tickable>, because Java's generics are invariant.

To have a method accept an ObservableList<Cue>, have the method parameter type be an Iterable<Cue> or use a bounded wildcard -- an Iterable<? extends Tickable>.

like image 107
rgettman Avatar answered May 25 '26 05:05

rgettman