Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is same erasure?

 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input:


Output:



What is the meaning of same erasure in Exception message???
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Why it is allowed???"
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
can you explain why the above code is allowed ?


Hmm i think because of polymorphism.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:Thank You...

you are welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic