....to produce the following output:
But instead it produces:
I was wrong in the output for the 1st & 3rd lines from the end.
I am following the following tutorial:
Where they say:
The following distinction between invoking instance methods on an object and accessing fields of an object must be noted. When an instance method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of the reference, that determines which method implementation will be executed. When a field of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determines which field will actually be accessed. This is demonstrated in the above program.
Because method1() is overridden, "supClass.method1();" will call method1() in OverridingClass. That's exactly what overriding means.
sc3 refers to OverridingClass (although sc3 is of the type SuperClassWithDifferentMethods).
Hence, according to the quote,
should have invoked the method5() that belongs OverridingClass (whether it is overridden or not) and produce:
[ March 06, 2007: Message edited by: Joseph Sweet ]
You have declared "SuperClassWithDifferentMethods sc3=...". The compiler will substitute your sc3.method5()-call with "SuperClassWithDifferentMethods.method5();".
Edit: Please note that static methods are not instance methods.
[ March 06, 2007: Message edited by: Anton Uwe ]