• 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

Generics from ExamLab

 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain me why the first method is ok and the second one gives a compile error?

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic