aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question in Generics -2 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 "Question in Generics -2" Watch "Question in Generics -2" New topic
Author

Question in Generics -2

Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Jul 11, 2005
Posts: 113
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

Joined: Jul 11, 2005
Posts: 113
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

Jyothsna I think these two links (which are of the same page ) will clear your doubt

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Why%20doesn%27t%20method%20overloading%20work%20as%20I%20expect%20it?

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Why%20doesn%27t%20method%20overriding%20work%20as%20I%20expect%20it?
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
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?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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

Joined: Jul 11, 2005
Posts: 113
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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40


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

Joined: Jul 11, 2005
Posts: 113
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question in Generics -2