aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some doubts about generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Some doubts about generics" Watch "Some doubts about generics" New topic
Author

Some doubts about generics

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 300

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


Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 801
    
    1
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

Joined: Mar 28, 2012
Posts: 300

Thanks for reply
but this was of no help
Alexandre Leveille
Greenhorn

Joined: Jan 26, 2013
Posts: 12

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.
 
subject: Some doubts about generics