public class MyClass {
public static void main(String args[]) {
class MyClassA {
private String name = "testA";
public String getName() {
return name;
}
public void printName() {
System.out.println(this.getName());
}
}
class MyClassB extends MyClassA {
private String name2 = "testB";
public String getName() {
return name2;
}
public void printName() {
super.printName();
System.out.println(getName());
}
}
MyClassA classA = new MyClassA();
MyClassB classB = new MyClassB();
System.out.println("Print name in A:");
classA.printName();
System.out.println("Print names in B:");
classB.printName();
}
}
Output is:
Print name in A:
testA
Print names in B:
testB
testB
My question is why it printed "testB" both times in the end. I would have expected it would be "testA testB" because the first "printName" in MyClassB called the super method.
Is it possible to make it work so that it prints "testA testB"?