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 Generics from ExamLab 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 "Generics from ExamLab " Watch "Generics from ExamLab " New topic
Author

Generics from ExamLab

Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

Can you explain me why the first method is ok and the second one gives a compile error?


SCJP6 | http://programmerutilities.blogspot.com/
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Mark Moge wrote:Can you explain me why the first method is ok and the second one gives a compile error?



yes, Java does not support return-type-based method overloading. Java methods only differ by arguments, not by return type. So, to the compiler the useMe() method and the useMe() method are exactly the same.

That is why the compiler says: useMe() is already defined in A
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96


after commented the first method there is still a compile error. And my question is why A<? super K> is ok and A<? extends K> is wrong.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Mark Moge wrote:
after commented the first method there is still a compile error. And my question is why A<? super K> is ok and A<? extends K> is wrong.


That compiles for me and it should because useMe() is set to return something that extends K which in turn is something that extends Number.
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

this code

but <K> at front hides <K extends Number> from the class definition and gives me a compile error

Is this code (with <K>) compiles without any problem?
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Mark Moge wrote:this code

but <K> at front hides <K extends Number> from the class definition and gives me a compile error

Is this code (with <K>) compiles without any problem?


it compiles for me.

the compiler will not even warn about the hiding, but the Eclipse IDE will warn about this.

what compiler / java version are you using that is not compiling this code?
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

I have checked again and I can compile it too but my IDE (netbeans) gives me warnings which look like a compile error.
Anyway I dont understand why one method is ok and second one isn't. And if sth is super of K (A<? super K>) then it doesn't have to extends Number (like in a class declaration).
Rageni Kalra
Greenhorn

Joined: Jan 29, 2014
Posts: 10
Hi,

I am using java 1.7 and i cannot compile this:



I get this error:

A.java:5: error: type argument ? extends K#1 is not within bounds of type-variab
le K#2
abstract <K> A<? extends K> useMe();
^
where K#1,K#2 are type-variables:
K#1 extends Object declared in method <K#1>useMe()
K#2 extends Number declared in class A
1 error

Can anyone help what is wrong?
 
 
subject: Generics from ExamLab
 
Similar Threads
confused with generics
Generic classes and Methods Problem
Confused over generics
Generic Declaration
Examlab generics question