This is a question from UW CSE 143. I am just studying past exams to get better at Java.
public class Box extends Pill {
public void method2() {
System.out.println("Box 2");
}
public void method3() {
method2();
System.out.println("Box 3");
}
}
public class Cup extends Box {
public void method1() {
System.out.println("Cup 1");
}
public void method2() {
System.out.println("Cup 2");
super.method2();
}
}
Box var3 = new Cup();
Question:
What is the outprint if var3.method3() is called?
I have no idea why the answer is Cup 2/Box 2/Box 3
Where is cup 2 from? I get the dynamic type is Cup. But if Cup class does not have method3, so it goes to the parent class for method3.