sudip Kumar wrote:
Do you mean that static methods and static variables only or even instance variables...
Runtime polymorphism in
Java applies only to non-private, non-static, non-final methods. It does not apply to anything that is static, private, or final, and it does not apply to any member variables.
So, if we have
and then we refer to
p.X, then if X is a member variable, or if it's private, static, or final, it is determined at compile time that we will get the X from whatever type reference "p" is declared to be--in this case, Parent. Only if X is a non-private, non-static, non-final method will we get X from the actual object that p refers to at runtime.