| Author |
kb QUESTION PG 617
|
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:
|
Matt
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: 774
|
|
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
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hey Dinesh, It would be good to understand what part of the question's answer was confusing? Thanks, Bert
|
Eliminate fossil fuel subsidies. (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 ~~♥๑۩۞۩๑
|
 |
 |
|
|
subject: kb QUESTION PG 617
|
|
|