In the
Java language the behaviour of 'overriding' methods and fields are different. At line 8 of the code example, the compiler associates the getSize method with the
class of b. That is, the getSize method of Runtime will be invoked. This is called dynamic binding and is how Java implements polymorphism.
However, for field declarations dynamic binding is not applied. For the size field the compiler will associate the field with the
class type of b (Base). Therefore when the program is run the code at line 8 displays
10,100.
In fact, because of the difference in behaviour it is not correct to use the term 'overriding' for fields. Instead if a class declares a field, then the declaration of that field is said to
hide any and all fields with the same name in the superclasses of the class.
Hope this explanation clarifies the example code.
Thomas De Vos
[ October 05, 2003: Message edited by: Thomas De Vos ]
[ October 05, 2003: Message edited by: Thomas De Vos ]