wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashSet add() method override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashSet add() method override" Watch "HashSet add() method override" New topic
Author

HashSet add() method override

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, Why can't I do this? Error is in the comments.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Harikrishna Gorrepati wrote:Hi, Why can't I do this? Error is in the comments.



It is not an override, because the super class add method takes an E instance as it's it parameter -- and in your example, it takes a Person object. So, technically, it should be a overload, one for Person and one for Object.

Unfortunately, it can't be an overload either. Because of type erasure, both methods come out to the same signature.

So.... hence, a compile error.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

add(E) is different from add(Object)


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Replace the Object with the Person .
It wil compile .
Prashant Shiralkar
Greenhorn

Joined: Apr 13, 2010
Posts: 19
Henry Wong wrote: Because of type erasure, both methods come out to the same signature.

Here, do you imply: add(E e) and add(Object o)? If so, does that mean, after "type erasure", add(E e) of HashSet<E> looks like add(Object o)?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3574
    
  14

Yes.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

As stated above, the problem is related to Generics and not collections.

Have a look at Hashset's add method:


Use the following code:



Java Experience
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


It will work with replacing Person with Object as well like below.



http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Javin Paul wrote:It will work with replacing Person with Object as well like below.

Your code will work but logically, if a class needs to extend a specific type of HashSet, then the code should be


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashSet add() method override
 
Similar Threads
kb QUESTION PG 617
Another Generics question
inheritance / generics doubts
Q12 K&B-Generic
A Generics and Collections Problem