File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Generics(Quite Urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Generics(Quite Urgent)" Watch "Regarding Generics(Quite Urgent)" New topic
Author

Regarding Generics(Quite Urgent)

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
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 ]

Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
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

Joined: Dec 15, 2007
Posts: 44
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Generics(Quite Urgent)