I am facing a problem in generics inheritance. I wrote foll 2 classes couldn't figure out why getting the compiler error. I read Angelika site regarding overriding but couldn't understand the foll :-----------
//GENERIC SUPERCLASS
class superc<T>{
void set(T arg){}
}
//GENERIC SUBCLASS
class subc extends superc<Number>{
void set(Object rr){}
}
On compiling above code I get foll compiler error. :-
superc.java:10: name clash: set(java.lang.Object) in subc and set(T) in
superc<java.lang.Number> have the same erasure, yet neither overrides the other
class subc extends superc<Number>{
^
1 error
CAN ANYONE HELP ??
This message was edited 2 times. Last update was at by Soniaa Agarwal
superc.java:10: name clash: set(java.lang.Object) in subc and set(T) in
superc<java.lang.Number> have the same erasure, yet neither overrides the other
The error message is pretty clear. You have two overloaded methods set(T) and set(Object). These methods are overloading -- not overriding. Yet, after type erasure (generics is compile only), the methods have the same signature.
It is not possible for two methods with the same signature, to be overloaded methods.
Soniaa Agarwal wrote:
Thanks Henry for the prompt reply but I am still not getting it. How come
methods set(T) and set(Object). are overloaded methods.
Why does the following work :
class superc<T>{
void set(T arg){}
}
class subc extends superc<Number>{
void set(Number aa){}
}
Hi Sonia,
Can you pleaseadd why did void set(Number aa){} work?