This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some doubts about generics

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.


A non generic class extending a generic class without passing the Type parameter to super class X
What i think is , it compiles because compiler sees it as class Y extends X<Object>
Am i right ?

2.



Then why does this compile.


Looking for answer that i can understand
 
Himai Minh
Ranch Hand
Posts: 1296
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generic was not available until JDK 1.5.
For backward compatibility, it compiles when you use a generic class in a non-generic way.
For example,we are supposed to use HashMap in this way : HashMap<String, String> or any other type. But it compiles if you put HashMap h = new HashMap();
You will get a warning, but it compiles in JDK 6.
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply
but this was of no help
 
Alexandre Leveille
Greenhorn
Posts: 12
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it compiles because compiler sees it as class Y extends X<Object>


No, it works to maintain backward compatibility.

You just created the classes S and V, but most of the time you'll have to deal with legacy code.

For example, last year you downloaded ms-util.jar from Microsoft.com. They had this awesome class S that did a lot of thing:


You decided on your project to extend that class to add some functionality:



Everything is fine, right? No generics so it is simple ;)

However, some Monday morning, a user reports a bug with your application. You investigate a bit and you find that the bug is in the class S... but you are in luck, it has been fixed in the latest version of ms-util.jar. So you head to Microsoft.com and download the newest version of ms-util.jar.

When you open (and decompile) the new ms-util.jar, here's what you find:


Microsoft has changed their classes to use generics. Awesome.... but will it break your code? Remember, your class V that extends S does not use generics and even pass a non-typed list to their methods!!

Sun made it that your old code (class V) would continue to compile. If they hadn't, you would have needed to recode all your classes!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic