aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can a child get stuff that is put in a parent's pocket? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can a child get stuff that is put in a parent Watch "How can a child get stuff that is put in a parent New topic
Author

How can a child get stuff that is put in a parent's pocket?

Bing May
Greenhorn

Joined: Dec 26, 2011
Posts: 8
This might be a stupid question ;-/, but the results from the code below really confused me. In the addit(Person p) method, it looks the Person object is added to a parent of Group g (i.e. a Person HashSet) via super.add(p). But g also gets the Persons that are added to super. I suspect my understanding of the relationship between g and super is not right. Can anybody give me some help here? Many thanks.

--- Bing


import java.util.*;

public class Group extends HashSet<Person>{
public static void main(String[] args) {
Group g = new Group();
g.addit(new Person("Hans"));
g.addit(new Person("Lotte"));
g.addit(new Person("Jane"));
g.addit(new Person("Hans"));
g.addit(new Person("Jane"));
System.out.println("Total:" + g.size() + " " + g);
}

public boolean addit(Person p) {
System.out.println("Adding:" + p + "; super size is " + super.size());
return super.add(p);
}
}

class Person {
private final String name;
public Person(String name){this.name=name;}
public String toString(){return name;}
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

The "relationship" between those two things is that there is only one thing. The Group object IS the HashSet<Person> object -- they aren't two separate objects. That's what it means for class A to extend class B, it means that every A object is also a B object.
Bing May
Greenhorn

Joined: Dec 26, 2011
Posts: 8
I see. Thanks for the explanation. So I just did a quick test and changed super to this, the code gave exactly the same results.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Yes, it would. (Group doesn't override the "add" method so its "add" method is the "add" method from its parent class, which is what "super" means.) I was wondering why they did it that way -- maybe it was supposed to illustrate some point. Could you tell us where the example came from?
Bing May
Greenhorn

Joined: Dec 26, 2011
Posts: 8
The original problem is from the MasterExam quiz set for SCJP 6.0, as shown below. I made two changes in the code I posted yesterday: 1) from add(Object o) to add(Person p), otherwise it won't compile; and 2) from add(...) to addit(...), which was just for the testing purpose.

I also tried commenting out the defition of add(Person p) method in the code below. By all the testing, I now understand why super is used here. Thanks again for the help, Paul.

--- Bing


public class Group extends HashSet<Person>{
public static void main(String[] args) {
Group g = new Group();
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total:" + g.size());
}

public boolean add(Object o) {
System.out.println("Adding:" + o);
return super.add(o);
}
}

class Person {
private final String name;
public Person(String name){this.name=name;}
public String toString(){return name;}
}

Which of the following occur at least once when the code is compiled and run? (Choose all that apply)
A Adding Hans
B Adding Lotte
C Adding Jane
D Total: 3
E Total: 5
F The code does not compile.
G An exception is thrown at runntime.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can a child get stuff that is put in a parent's pocket?