IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  Generics Confusion in Overriding a method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark " Generics Confusion in Overriding a method" Watch " Generics Confusion in Overriding a method" New topic
Author

Generics Confusion in Overriding a method

Soniaa Agarwal
Greenhorn

Joined: Jun 12, 2009
Posts: 22
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 ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 14606

CAN ANYONE HELP ??


First, I am assuming "foll" means "following"?

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Soniaa Agarwal
Greenhorn

Joined: Jun 12, 2009
Posts: 22

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){}

}
Soniaa Agarwal
Greenhorn

Joined: Jun 12, 2009
Posts: 22


Is Generics Inheritance on the SCJP 5.0 exam.

Where should I study it from besides Angelika Langer.
suavedeep kaur
Ranch Hand

Joined: Jun 02, 2008
Posts: 36
Go for Kathy & bert bates SCJP book its really good ......telling you from my experience sonia



Suavedeep kaur
SCJP
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 754

Hi Soniaa,
If you get more knowledge on Generics then its an advantage for you.
You can refer Sun Java Tutorial.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Soniaa Agarwal
Greenhorn

Joined: Jun 12, 2009
Posts: 22


Thanks Henry .

Finally I think I understand that its overloading and not overriding.

reji singh
Ranch Hand

Joined: Apr 06, 2009
Posts: 52
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?
 
 
subject: Generics Confusion in Overriding a method
 
Threads others viewed
Generics
Generics
Generics & overriding
Generics
Generics
developer file tools