This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes kb QUESTION PG 617 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "kb QUESTION PG 617 " Watch "kb QUESTION PG 617 " New topic


Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486

The above code doesn't complie and gives error . can't add person
Please advise and correct if any code added in code.

Thanks<br />Dinesh
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Because you're extending a generic version of HashSet, you need to override the add method with a Person argument, not an Object, like this:

Inquisition: open-source mock exam simulator for SCJP and SCWCD
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
So i need to correct my add method

public boolean add(Person o)
return super.o;

Could please teel wht super.o will do..

Correct me if i am wrong
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
You need super.add(o), rather than super.o. That will call the implementation of add() in HashSet, the superclass of Group, and will add the person to the HashSet. There's not much point to the override, as the HashSet add() method would have been called in any case.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

name clash: add(java.lang.Object) in Group and add(E) in java.util.HashSet<Person> have the same erasure, yet neither overrides the other
public class Group extends HashSet<Person> {

I got above error while compiling please explain it

I keep signature of the method public boolean add(Object o)as it is
while compiling

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
Bert Bates

Joined: Oct 14, 2002
Posts: 8803
Hey Dinesh,

It would be good to understand what part of the question's answer was confusing?



Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Signature of your add method must be like this .

public boolean add(Person o)
return super.add(o);

Because HashSet is instantiated with type parameter Person .
Hence add method of HashSet class will take Person object only .

SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
I agree. Here's the link:
subject: kb QUESTION PG 617
Similar Threads
A Generics and Collections Problem
Generic Doubt
Another Generics question
K&B Chapter 7 Question # 12