aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic classes and Functions Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic classes and Functions Problem" Watch "Generic classes and Functions Problem" New topic
Author

Generic classes and Functions Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634





Line 1 compiles fines individually but gives error when line 2,3 are also used

Why?

-----------------------------------------------



can someone tell me why doesn't all the function
declaration in the above class fails to compile ?

---------------------------------------


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Line 1 compiles fines individually but gives error when line 2,3 are also used. Why?

You cannot overload methods based on different generic types. Like I cannot have these methods in the same class
This limitation is due to type-erasure as at runtime these two methods would be completely same...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Ankit Garg wrote:
This limitation is due to type-erasure as at runtime these two methods would be completely same

ok,at runtime typing information doesn't exist, but at compile time,compiler can able to identify the different types(type parameter),then why it doesn't compile?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

At compile time the compiler can identify which method will be called but how will the JVM identify which method to call at runtime?? It will have two methods with the exact same signature, which method would it call??
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Ankit Garg wrote:At compile time the compiler can identify which method will be called but how will the JVM identify which method to call at runtime?? It will have two methods with the exact same signature, which method would it call??

hmm..ok,got the concept,JVM(instance of JRE) which doesn't allow program to compile here in this case.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Ankit wrote:
at compile time the compiler can identify which method will be called but how will the JVM identify which method to call at runtime?? It will have two methods with the exact same signature, which method would it call??



I didn't get it.
can anyone explain


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic classes and Functions Problem