here is java code
class Cup {
public String sayColor() {
return "i have a color .";
}
}
class TCup extends Cup{
public String sayColor(){
System.out.println(super.getClass().getName());
return super.sayColor()+"color is tee green.";
}
}
class MyTCup extends TCup {
public String sayColor(){
System.out.println(super.getClass().getName());
return super.sayColor()+"but brushed to red now!";
}
}
class Test {
public static void main(String[] args) {
Cup c = new MyTCup();
System.out.print(c.sayColor());
}
}
and Test print
MyTCup
MyTCup
i have a color .color is tee green.but brushed to red now!
question 1: At the runtime, the object C's type is MyTCup,but it always can call the super method .Is there is a method stack in the memory within MyTCup after initialing the object ,and then can call through at runtime like the code ?
question 2: There is no way to call the super method in other objects .As i know ,c++ can cast to call parent method at any time.Why java design like this?