• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Generics(Quite Urgent)

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic