Please help me with this:
If Lion IS-A Animal and given Cage<T>:
Cage<? extends Animal> c = new Cage<Lion>(); // ok,
but
Set<Cage<? extends Animal>> cc = new HashSet<Cage<Lion>>(); // not ok
What I don’t see here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When assigning to a variable (
Set<T>) with a non-wildcard generic typeT, the object being assigned must have exactlyTas its generic type (including all generic type parameters ofT, wildcard and non-wildcard). In your caseTisCage<Lion>, which is not the same type asCage<? extends Animal>.What you can do, because
Cage<Lion>is assignable toCage<? extends Animal>, is use the wildcard type: