I would like to know why the output is 5 and not 6. Because I thought that in this code the polymorphism had been applied then the call c1.getObject() would return the object B.
Can anybody help me?
Ok I see the thinking here, but isn't it the rule that instance variables cannot be overridden?
Certainly at compile time the compiler checks the reference type which is of the covariant type where the call to getObject returns an A: However, at runtime the JVM checks the Object type which is then a SubCovariantTest and thus the getObject returns an object of type B. However, as the SubCovariantTest instance variable x does not successfully override (as its an instance variable) the Superclass x value is still used and hence 5 is returned?
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards