Forums Register Login

Generics from ExamLab

+Pie Number of slices to send: Send
Can you explain me why the first method is ok and the second one gives a compile error?

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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?
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1115 times.
Similar Threads
Generic classes and Methods Problem
Examlab generics question
Confused over generics
Generic Declaration
confused with generics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:10:15.