class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
public class Mixer<A extends Animal>{
public <C extends Cat> Mixer<? super Dog> useMe(A a, C c){
//return new Mixer<Object>();//KO
return new Mixer<Animal>(); //OK
}
}
The return parameter is Mixer<? super Dog> so if is a defined with a lower bounded wildcard
Why do I have a compiler error when I return a Mixer<Object> and there is no compiler error with Mixer<Animal>?
The problem is not in the return type of your method, rather it is the
Generic Typebound to your classMixer.Let’s see what went wrong: –
The return type
Mixer<? super Dog>means, you can return anyMixerof typeDogor asuper-typeof Dog, may beAnimal.So, both the
returnstatments would have worked fine, because, bothAnimalandObjectis asuper-typeofDog.But, the reason why the
first onedoes not fits in is because, you have declared your class as: –So, you have
boundyourtypethat can be associated withMixerclass to eitherAnimalor itssubtype.Now, since,
Objectis not a subtype ofAnimal, you can’t just create: –So, you can create instances of your class like:-