stack - why polymorphism in java call the methods from the superclass -


///example code here superclass{      method1(){        print(do1);      }     method2(){}   } subclass extends superclass{    ///override method1  method1(){        print(do2);     }  method3(){}  } 

i have question polymorphism in java, when superclass s =new subclass(). "s" object invoke method in superclass, when override method happened, "s" point override methods.

update: so, question create heap address reference "s", if can compiled , running in end. if superclass created it, why new subclass() rather new superclass(). if subclass created it, why cannot use s.method3().

s can call method3. have cast first. if allowed s call method3 superclass, wouldn't make sense because not superclass can call method3.


Comments