Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Generics(Quite Urgent)

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case 1)HashSet<String> hs = new HashSet<String>();
hs.add("scjp");
hs.add("exam");
HashSet<Object> s = new HashSet<Object>();
s=hs;
System.out.println(s);
Case 2)
HashSet<Animal> hsA = new HashSet<Animal>();
HashSet<Dog> hsD = new HashSet<Dog>();
hsA=hsD;
Above assume Dog extends Animal correctly.
Above code gives compiler error at Case1) s=hs;
Case 2) hsA=hsD;
Why do you get complier errors, though left side of assignment is supertype of right side(Eg s is of type object which is ofcourse superlcass of all and in case 2 hsA(animal) is supertype of hsD(dog)).

Above probably would have worked for arrays....but why not for Generics.

Also why above is not allowed even if you are not ADDING anything....
[ December 21, 2007: Message edited by: Maan Shenoy ]
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maan Shenoy,

Guys correct me if I'm wrong.

HashSet<String> hs = new HashSet<String>();
hs.add("scjp");
hs.add("exam");
HashSet<Object> s = new HashSet<Object>();
s=hs;

gives a compiler error because polymorphism does not work with generics. since the reference variable s is TYPED for type object, you can not assign it to hs which is TYPED for type String. The angle bracket information <E> is available at compile time only. The above statement s=hs is like writing

HashSet<Object> s = new HashSet<String>();

which is not allowed. Polymorphism applies to base classes only i.e
Set<Object> s = new HashSet<Object>(); is fine.

if you remove the <Object> from the reference variable declaration, the compiler error is gone (Same for the Animal case).

Hope I'm write and it helped.
Kind Regrads.
Hasnain Javed Khan.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maan Shenoy,

Guys correct me if I'm wrong.

HashSet<String> hs = new HashSet<String>();
hs.add("scjp");
hs.add("exam");
HashSet<Object> s = new HashSet<Object>();
s=hs;

gives a compiler error because polymorphism does not work with generics. since the reference variable s is TYPED for type object, you can not assign it to hs which is TYPED for type String. The angle bracket information <E> is available at compile time only. The above statement s=hs is like writing

HashSet<Object> s = new HashSet<String>();

which is not allowed. Polymorphism applies to base classes only i.e
Set<Object> s = new HashSet<Object>(); is fine.

if you remove the <Object> from the reference variable declaration, the compiler error is gone (Same for the Animal case).

The angle bracket information <E> MUST be the same on the left and right side of the = sign.

Hope I'm write and it helped.
Kind Regrads.
Hasnain Javed Khan.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic