aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q12 K&B-Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Q12 K&B-Generic" Watch "Q12 K&B-Generic" New topic
Author

Q12 K&B-Generic

nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34

it has options


didn't get the explaination given that
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
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
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

public group extends HashSet<Person>

so at this point you will be able to add only with Person objects on the group class.


if you take a look on how you use comparable and comparators you would get a better understand too ;)


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q12 K&B-Generic
 
Similar Threads
what is the correct answer?
HashSet
A Generics and Collections Problem
Generics K&B doubt
Another Generics question