supriya riya wrote:
I have confusion regarding the output which is: sub1 2
But i can't understand why obj.i prints value 1 rather than 2
as the actual type of obj is B at run time ( it calls obj.myInt of class B???). So it must print sub2 2.
Not getting overriding concept right.Confused what gets called subclass /super class method depending on reference type.
Polymorphism only applies to method calls -- and specifically, only non-static method calls where overriding is taking place. For variables (either static or instance) and for other types of methods calls (such as static methods or private methods), it is completely resolved at compile time.
Henry