• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question in Generics -2

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source :http://nikojava.wordpress.com/2008/10/09/scjp-mock-exam-for-generics/#scjp-generics-25
Question : Does the following code compile?


My answer : The code compiles as it differs in the argument list.
On eclipse the above code doesnt compile.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jyothsna yes the code will not compile. I want to say a few things here. First of all don't use eclipse for your practice for SCJP. There are two reasons for this. 1. Eclipse helps you with the syntax and API which will make you kinda dependent on it and you'll not be able to notice the minute details of the API which might harm you in the exam. 2. The exam is designed based on what the result of a code will be on the command line. I have seen a few occurrences on this forum itself where eclipse works differently than the command line javac tool.

The code will not compile because the two methods have identical syntax for the JVM but not for the compiler. After type erasure, JVM will think that both the methods are the same so there is overriding, but for the compiler the two methods have different type of the list. So to avoid any runtime unexpected behaviors, the compiler will reject this code...
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is something to do with "type erasure", at the compilation, compiler erase the type, and the code resulted something like this,


and this is what compiler saw, and throws error, complaining "name clash" ,
This is totally my derivation of what I understood as "Generics", so correct me, if I were wrong
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sagar you are right. Before type erasure, both the methods are different so there is no overriding and thus the class has two methods named say, one taking a List<Integer> and the other List<String>. But after type erasure, both the methods will have the same signature and thus the method say in class Child will actually override the method say in class Parent which is not true. Thus you get an error at compilation...
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,

Thanks a lot for your inputs.

Ankit ,

I shall take care that I dont use eclipse for the practising.
I shall use regular notepad.

thanks,
Jyothsna
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit ,

One Question here .when there is type erasure , the two methods should work right?
why should it show compiler error?

thanks,
Jyothsna
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit ,

When I run the following code using command line its gives me error?
I not able to get whats wrong with the code?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I run the following code using command line its gives me error?



http://faq.javaranch.com/java/TellTheDetails



One Question here .when there is type erasure , the two methods should work right?
why should it show compiler error?



With generics, those two signatures are different -- so, in order for type checking to be done, those two methods must be considered as overloading, and *not* as overridding.

With type erasure, those two methods can't be overloaded, hence, the type conflict error.

Henry
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Can you give me more details . I am not getting it
When type erasure happens , then it should work right?

please confirm my understanding.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jyothsna Panchagnula wrote:Hi Henry,

Can you give me more details . I am not getting it
When type erasure happens , then it should work right?

please confirm my understanding.




Again....

http://faq.javaranch.com/java/TellTheDetails

If you don't tell us what is it about the last explanation that you don't understand -- we can't elaborate.

Henry
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

The way I understand is that from the code in the posts above , the parent class has a method (ie which takes String and the child class has overrloaded the method allows a type which implements/extends String class.
As the String class is declared final , so the code doesnt work.

Also the following code didnt work when I compiled

My understnading :- The code should work because , the child class accepts the type String and its Super Classes . The Parent class accepts only type string.

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, let's explain this code... And explain why it *must* be overloading and *not* overridding.



The Parent class takes a list of String type -- and only the String type. It is not allowed to contain anything else. In the Child class, the list can contain other objects. It is a list of an unknown type that can be either String or Object.

The signature is different... The child is overloading the method. It is *not* overriding the method. With the Child class, I can create a List<Object> and add everything from StringBuffers, File objects, Thread objects, etc.


To repeat again, the Child class is *overloading* the method. It is *not* overriding the method. Overriding the method would be violating the type check for the Parent class....

Now, with erasure, both methods become a single parameter of List. This means that it is not possible of the Child to overload the method, it is overriding the method. Overriding the method violates the type check... hence, the compiler will not allow it.

Henry
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thank you for the detailed explanation. I am getting stuck up here.

As the method in the child class "overloads" the method , the mehtod should compile rite.
Here is where I am banging my head. I am not able to understand , if the method is overloaded,
then why doesnt it compile?
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jyothsna Panchagnula wrote:

As the method in the child class "overloads" the method , the mehtod should compile rite.
Here is where I am banging my head. I am not able to understand , if the method is overloaded,
then why doesnt it compile?



Read carefully what Henry wrote:

Now, with erasure, both methods become a single parameter of List. This means that it is not possible of the Child to overload the method, it is overriding the method. Overriding the method violates the type check... hence, the compiler will not allow it.


Here Child class NOT overloading the method, but overriding, and its because "Type Erasure" during compilation of generics code.


Method Overloading:
same method name, with different parameters, like



Method Overridng:
Method is same, same name, same parameter. like



From this above discussion, can I derive this rule,

While compilation, Do not consider <TYPE>, for Generics !


Any comments ?
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic