Just declare your method with:
@Override protected <T extends Magicable> List<T> getMagicables() { List<T> list = ... return list }
If you really want this:
@Override protected List<Magican> getMagicable() {..}
you may need to declare your generic T in defintion class
public abstract class AbstractKlass<T extends Magicable> { protected abstract List<T> getMagicables(); }
then in your subclass:
public class MySubClass extends AbstractKlass<Magican> { @Override protected List<Magican> getMagicables() { ... } }
- Second case
A compilation error is normal because <? extends Magicable> <? extends Magicable> from a method signature means that it doesn't matter to you whatβs inside your list from the moment you can view these elements in the same way as Magicable. Making a call
List<T> list = getMagicables();
You want to take care of type T without knowing it. In other words, there are 3 uses: T is magic (OK), T is a wizard (wrong because getMagicables can return a Witch list), and T is a witch (also incorrect).
- Why am I using
? extends Magicable ? extends Magicable instead of just Magicable on lists
Because List<Magician> is a subtype of List<? extends Magicable> List<? extends Magicable> , but not a subtype of List<Magicable> . This is useful for parameter methods.
public void doIt(List<? extends Magicable> list) {
can be used as
List<Witch> list = ... doIt(list);
But if you have
public void doIt(List<Magicable> list) {
You cannot use it as
List<Witch> list = ... doIt(list);
maxime hochet
source share