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 What is same erasure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is same erasure?" Watch "What is same erasure?" New topic
Author

What is same erasure?

Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Input:


Output:



What is the meaning of same erasure in Exception message???
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

erasure is process which removes all generic type<> information at compile time,so here i can see it gives fake impression of overriding but it is not,hence name clash.


Tell the difficulties that i am difficult.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Gaurangkumar Khalasi wrote:Input:


if you consider erasure, it should not give error. but, lets pretend it compiles.

now consider below code:


and below code is allowed: do you understand why it is?


Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

This is also called type erasure, because it removes all type information on compile time. so at runtime List<String> is equal to List or raw type. this is done to support code written prior Java 5. You can also check http://javarevisited.blogspot.com/2011/09/generics-java-example-tutorial.html for more details.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Thank you to all, who have given response...

Seetharaman Venkatasamy wrote:
below code is allowed: do you understand why it is?

No...Why???
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Seetharaman Venkatasamy wrote:below code is allowed: do you understand why it is?


No, i don't. The above code compiles and runs...but how???
Based on type erasure concepts, after compilation Collie1 class will has two methods

So, "name clash" error have to be given by compiler, haven't it???
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Seetharaman Venkatasamy wrote:
Gaurangkumar Khalasi wrote:Input:


if you consider erasure, it should not give error. but, lets pretend it compiles.

now consider below code:


and below code is allowed: do you understand why it is?




can you explain why the above code is allowed ?


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
"Why it is allowed???"
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
can you explain why the above code is allowed ?


Hmm i think because of polymorphism.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:
now consider below code:
[code=java]
Dog dog = new Collie1();
dog.check(new ArrayList<Cat>);//compiler will allow this because new ArrayList<Cat> can reference to List



I've doubt here, you said compiler should allow this but i don't think it should be allowed because ArrayList<Cat> can reference to List but type of Collie not Cat and this is work of Generic declaration if it'll be ignoered then what is the meaning of Generic type declaration

List<Collie1> l=new ArrayList<Cat>() how it is possible ? while i think it is ok to List<Collie1> l=new ArrayList<Collie1> ()

I got it later
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
saloni jhanwar wrote:Hmm i think because of polymorphism.

Can you explain in detail???

one is List and other is List<Collie1> arguments...
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Gaurangkumar Khalasi wrote:
saloni jhanwar wrote:Hmm i think because of polymorphism.

Can you explain in detail???

one is List and other is List<Collie1> arguments...


Do compile and see, i didn't this. i said i think so you can also think, don't worry ranch wont charge you for it.I didn't consider much at that time and said so because of fake impression of overriding after type information is removed that was confusion,i was just guessing the reason.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Gaurangkumar Khalasi wrote:"Why it is allowed???"


Gurpeet already asked same question, you just can't do demand to get answer immediately so repetition and bold font wont magic much,and by the way see coderanch rulePatienceIsAVirtue
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
saloni jhanwar wrote:
Gaurangkumar Khalasi wrote:"Why it is allowed???"

Gurpeet already asked same question, you just can't do demand to get answer immediately so repetition and bold font wont magic much,and by the way see coderanch rulePatienceIsAVirtue

I have given another response but later i have got that it is incorrect and the post becomes the same as gurpreet...so it is because of mistake...

saloni jhanwar wrote:Do compile and see, i didn't this. i said i think so you can also think, don't worry ranch wont charge you for it.

OK, Thank You...i will...
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Seetharaman Venkatasamy wrote: do you understand why it is?

Yes, i do...Just go through my understanding and give any precious comments if i am going wrong..

And the following code is the reverse thinking of it(i.e. if we do not want that kind of error).
So it will definitely compiled and run...

saloni jhanwar wrote:Hmm i think because of polymorphism.

And Yes, it is a polymorphism...
Thank You.
It's take much time to understand but now i am happy...
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Gaurangkumar Khalasi wrote:


And the following code is the reverse thinking of it(i.e. if we do not want that kind of error).
So it will definitely compiled and run...





Do you think it'll work ??


Reverse will work but not with code used in first example.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
saloni jhanwar wrote:
Reverse will work but not with code used in first example.


Yes, True...
Gaurangkumar Khalasi wrote:
the reverse thinking of it(i.e. if we do not want that kind of error).

It means that one method that is passed at compile time, it will have to be run nicely without errors (especially, declaration or syntax related)...

Now Consider following code,

And, you can write something like below in it which will be successfully compiled and after it successfully ran as opposite to previous example...


Hope it will solve your doubt...
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Gaurangkumar Khalasi wrote:
Hope it will solve your doubt...


I've not any ,you have ok that's why i was explaining you.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Gaurangkumar Khalasi wrote:
Yes, i do...Just go through my understanding and give any precious comments if i am going wrong..

finally
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
saloni jhanwar wrote:I've not any ,you have ok that's why i was explaining you.

OK. Thank You for your explanation...
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Seetharaman Venkatasamy wrote:
Gaurangkumar Khalasi wrote:
Yes, i do...Just go through my understanding and give any precious comments if i am going wrong..

finally


Thank You...
So i have understood right....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Gaurangkumar Khalasi wrote:Thank You...

you are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is same erasure?
 
Similar Threads
Type of the Generic List
The use of ? extends
Generics and ? symbol
Generic Problem
Question on type erasure. (just another generics question)