jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile error in generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile error in generics" Watch "compile error in generics" New topic
Author

compile error in generics

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
this code compiles.....but the code below this doesnt....why? and whats same erasure?




scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Thats called type erasure. I've posted some more information about it in your other topic.

That's because Set and SortedSet are 2 different interfaces and Set<Double> and Set<Boolean> are because of type erasure the same.
I've noticed that you have multiple topics regarding generics. Try to read some information about it. I highly recommend the book from
Kathy Sierra and Bert Bates "Sun Certified Programmer for Java 6 Study Guide". It's great. If kathy of Bert is reading this, thank you!


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I am sure you know that generics are only for compile time type safety, at runtime jvm has no information about this type safe declaration.
jvm sees the code as it used to see prior to generics....that is after the program is compiled all the type safe declaration are removed this is called type erasure.

I hope I am correct.


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
got you... thanks
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521


then why doesnt this compile ? i know you said that there will be erasure but the compiler says that they cant be overloaded....can you clear this one? and even this one
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
the compiler error I am getting is:
Name clash: The method say(List<Integer>) of type Child has the same erasure as say(List<String>) of type Parent but does not override it
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Raju Champaklal wrote:

then why doesnt this compile ? i know you said that there will be erasure but the compiler says that they cant be overridden or overloaded....can you clear this one? and even this one
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
Regarding the above example,

let us say i have a class grandchild which passes a linkedlist object to the parent say method and child say method... The list will accept it. No matter what you save inside the object the object nature is list only..(I hope you understand)

You give generics name only to save different datatypes in an object(This is very imporatant what is an object but a userdefined datatype)


To err is human,
To forgive is not company policy
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
i know but what does it mean? why cant they be overridden?
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
The reason I think is:
for compiler they are two different type safe lists so it is definitely not a legal override.

Its just a guess, actually I am really confused
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
then why not overloading? hmm maybe because of the erasure you said....
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
yes, probably we are right.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
if parent has (List list) and child has (List<String> list) it doesnt compile but if its the opposite it compiles? this is so confusing
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
It compiles, compiler is just giving you the warning. Because here it thinks that you are overriding the super class' method and it tells you that to refer to a type safe collection you should make this one also type safe with the same type.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ050

this link may help me
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
still not getting it....6 days left for exam
Anchit Herredia
Greenhorn

Joined: Jul 15, 2009
Posts: 26
Raju Champaklal wrote:this code compiles.....but the code below this doesnt....why? and whats same erasure?




I noticed that we haven't replied to this one yet. So here's the answer to your question:
In Java 1.5, while fetching the objects from any collection it would be returned as an instance of "Object class" (which is the simply father of all classes). Now we have to typecast this returned object to whatever actual object it was. For example, if we got a string object we would say:



But the problem with this was that if the object stored in the collection was not a String object we would get a runtime error. So in Java 1.6 we have the capability of generics. Here we tell the compiler before hand that our collection (HashSet in my example above) would be storing some particular type, such as string. So once the compiler knows that HashSet object stores only Strings it can point out any mistakes in the code where we are not storing String.

Coming back to your question, the generics checking happens during compile-time. So during compilation it is checked that you are only putting String objects into HashSetObject. However, after compilation the generic is removed. So if you have two methods with same name but different generics both will still have the exact same signature after compilation. But then there would be an error. We would two methods with exact same signature but different bodies. This cannot be allowed hence the compiler gives you that error.

Hope that answers. Here's some more info: Generics_in_Java

 
wood burning stoves
 
subject: compile error in generics