Generics
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Given the following:
Animal class
Dog class extends Animal class
Please explain to me why the following is not allowed: (gives compilation error)
Cage<Animal> cg1 = new Cage<Dog>();
I always thought it means that LHS indicates that Cage may contain objects of Animal class. And by doing the instantiation in RHS, we are specifiying that the objects added would be of type Dog. Since Dog is a subclass of Animal, it should not matter even if Dog is added to the Cage.
Thanks
Fiona
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
one rule you have to remember here that polymorphism is not apply for generic type.
if you provide as per below it will result compile error :
Cage<Animal> cg1 = new Cage<Dog>();
but these example is ok :
Cage <Animal> cg1 = new Cage <Animal>();
Cage<? extends Animal cg1 = new Cage <Dog>();
please check this one for further information
regards,
-Vierda-
SCJP 5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Please explain to me why the following is not allowed: (gives compilation error)
Cage<Animal> cg1 = new Cage<Dog>();
I always thought it means that LHS indicates that Cage may contain objects of Animal class. And by doing the instantiation in RHS, we are specifiying that the objects added would be of type Dog. Since Dog is a subclass of Animal, it should not matter even if Dog is added to the Cage.
In this case, the easiest way to explain why is with an example.
You are declaring a reference that points to a Cage<Animal> type -- this means that this cage can hold Animals. And Animals are Dogs, Cats, Horses, Kangaroos, Eagles, etc.... So, with the cg1 reference, you should be able to add any of those animals.
But you are assigning an object of Cage<Dog> type. This object can only hold Dogs. So, if the Java compiler allowed the assignment to occur, you would be able to put any Animal type into the Cage<Dog> object.
Henry
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Vierda Mila wrote:Hi N path,
one rule you have to remember here that polymorphism is not apply for generic type.
if you provide as per below it will result compile error :
Cage<Animal> cg1 = new Cage<Dog>();
but these example is ok :
Cage <Animal> cg1 = new Cage <Animal>();
Cage<? extends Animal cg1 = new Cage <Dog>();
please check this one for further information
regards,
-Vierda-
Thanks for your reply Vierda.
But what exactly does the syntax mean then? If it is to be explained in simple english..
As in,
Cage<Animal> cg = new Cage<Animal>();
Cage<? extends Dog> cg1 = new Cage<Dog>();
According to this syntax, cg can contain an object of Animal class. Then what does the type in RHS signify?
In the first case, the type in LHS and RHS is the same. Why is it not the same in the second case? Why can't we write the following
Cage<? extends Dog> cg1 = new Cage<? extends Dog>();
This is a little tough to explain!
But i hope you understand! All i mean to ask it, what do the types on each side indicate in plain English? Fiona
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
ok I'll try to explain
Cage<? extends Dog> cg1 = new Cage<Dog>();
Cage <? extends Dog> --> means that you can be assigned a collection that is subtype of Cage and type for<Dog> or anything that extends Dog, so below also correct :
Cage <? extends Dog> cg1 = new Cage <Poddle>(); --> according Poddle is subtype of Dog
In the first case, the type in LHS and RHS is the same. Why is it not the same in the second case? Why can't we write the following
Cage<? extends Dog> cg1 = new Cage<? extends Dog>();
well, the explanation for this is simple you CANNOT use wildcard notation (<?> or <? extends ClassName> or <? super ClassName>) in the object creation so new Cage <? extends Dog>(); --> will generate compile error.
please anyone correct me if I'm wrong. thanks
regards,
-Vierda-
SCJP 5
| I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











