Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Generics

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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-
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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?


 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi N Pats,

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-
reply
    Bookmark Topic Watch Topic
  • New Topic