Java Reference
In-Depth Information
When
show( )
is invoked on an object of type
B
, the version of
show( )
defined within
B
is used. That is, the version of
show( )
inside
B
overrides the version declared in
A
.
If you want to access the superclass version of an overridden method, you can do so by
using
super
. For example, in this version of
B
, the superclass version of
show( )
is invoked
within the subclass' version. This allows all instance variables to be displayed.
If you substitute this version of
show( )
into the previous program, you will see the fol-
lowing output:
Here,
super.show( )
calls the superclass version of
show( )
.
Method overriding occurs only when the signatures of the two methods are identical. If
they are not, then the two methods are simply overloaded. For example, consider this mod-
ified version of the preceding example: