TrainBeaser for iPhone
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

Generics Confusion in Overriding a method

Soniaa Agarwal
Greenhorn

Joined: Jun 12, 2009
Messages: 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 ??

This message was edited 2 times. Last update was at by Soniaa Agarwal

Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 9917

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
Messages: 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
Messages: 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
Messages: 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
Messages: 650

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

SCJP 5.0 97% JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - API - Generics FAQ - JLS - JVM Spec - Java FAQs
Soniaa Agarwal
Greenhorn

Joined: Jun 12, 2009
Messages: 22



Thanks Henry .

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

reji singh
Ranch Hand

Joined: Apr 06, 2009
Messages: 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?
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
hibernate profiler