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.