Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics from ExamLab

 
Mark Moge
Ranch Hand
Posts: 100
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain me why the first method is ok and the second one gives a compile error?

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic