But, Cindy, when running the original code posted by ZEESHAN AZIZ, the S2.s variable should have taken precedence over the S1.s variable, right? If I read your post right, that's what you were saying.
But our debate is why does the display() method (when called from an instance of S2) choose the S1.s variable instead of the S2.s variable?
I'm still confused as to why this happens. The only thing I can think of is that display() is not overridden in S2, so display() is taking the this.s variable and this is referring to S1, not S2? But doesn't that contradict
polymorphism concepts of being able to use the inherited methods from the superclass to apply to the instance of the subclass? Do you have to use the inherited variables as well, without shadowing them?
What if you wanted s to be an int in S2 and you wanted display() to print the int 2 instead of a string? (I know that ints convert to Strings for the purpose of the System.out.print() method, but with polymorphism, we shouldn't have to care.) Wouldn't it make sense to shadow that variable in S2 as:
int s = 2;
?
Confusing at best.
Thanks for answers.
April