///Example code here
Superclass{
method1(){
print(do1);
}
method2(){}
}
Subclass extends Superclass{
///override method1
method1(){
print(do2);
}
method3(){}
}
I have a question for the polymorphism in java, which is when Superclass s =new Subclass()
. the "s" object always invoke the method in the Superclass, but when the override method happened, the "s" will point back to the override methods.
Update: So, the question is who will create a heap address for the reference "s", if it can be compiled and running in the end. If the Superclass created it, why new Subclass() rather than new Superclass(). If the Subclass created it, why cannot use s.method3().