• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;}
}
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic