shalini saranya wrote:
So What happens if a method is invoked by a reference of its superclass which is assigned with object of its subclass
Note:The method which is invoked exists only in sub class and not in superclass
On a super class reference, you can only call methods defined in the super class. If not, the compiler shall stop you right away.
Now suppose we have a super class reference pointing to a sub class object, and we call a overridden method in it.
At the compile time, the compiler shall decide to call the super class's method,yes..... but at run-time if that method is overridden in the sub class, it shall be called instead : Run-time
polymorphism.
The method overriding is an example of runtime polymorphism.
Java virtual machine determines the proper method to call at the runtime.