F is correct. The problem here is in Group's add() method—it should have been
add(Person), since the class extends HashSet<Person>.
can you also explain why it works when we change add(object ) to add(Person)
i have question that isn't the return statement of add() method wrong is it possible we can use super anywhere else rather than first statement in a menthod
Sunny Mattas
Ranch Hand
Joined: Apr 22, 2008
Posts: 45
posted
0
Hi,
I think above line means Group class extends a HashSet class. But using <Person> we have made sure only Person is added to the HashSet.
But in above code we are calling add method of base class of Group class which in this case is HashSet and trying to add the Object (Remember compiler only knows about reference type not real object) using super.add(o) .This will not work as HashSet is only allowed to take Person types in this case.And we are getting compiler error.
i have question that isn't the return statement of add() method wrong is it possible we can use super anywhere else rather than first statement in a method
We can use super() super as first statement, which calls the construtor of parent class but super.add() makes sure
add method of parent class is called even if there is a same add method present in current class.super can be used anywhere except static methods or blocks to call the members of base class
Regards
Sunny Mattas
SCJP5
Regards
Sunny Mattas
SCJP5
Omar Al Kababji
Ranch Hand
Joined: Jan 13, 2009
Posts: 357
posted
0
in the class of the HashSet class you will be having something like this line of code
so when you extend the HashSet class by specifying the generic type, all the T objects gets replaced with Person, which was specified in the extends statement