Section 6 : Overloading,
Overriding, Runtime Type, and Object Orientation
code to invoke overridden or overloaded methods and
parental or overloaded constructors; and describe the
effect of invoking these methods.
Method overloading is one of the ways
that java implements polymorphism. When an overloaded
method is called, java uses the type and/or number of
arguments to decide which version of the overloaded
method to actually call. Overloaded methods may or may
not have different return types. When java encounters
a call to an overloaded method, it simply executes the
version of the method whose parameters match the
arguments used in the call.
However, this match need not always be
exact. In some cases java’s automatic type conversions
can play a role in overload resolution. Java will
employ its automatic type conversion only if no exact
match is found.
The important points to
difference in return type only is not sufficient to
constitute an overload and is illegal.
should restrict your use of overloaded method names
to situations where the methods really are
performing the same basic function with different
language treats methods with overloaded names as
totally different methods and as such they can have
different return types. However, since overloaded
methods perform the same job with different data
sets, they should produce same return type normally.
There is one particular condition, however, under
which it is sensible to define different return
types. This is the situation where the return type
is derived from the argument type and is exactly
parallel with the arithmetic operators.
Overloaded methods may call one another simply by
providing a normal method call with an appropriately
formed argument list.
class hierarchy, when a method in a subclass has the
same name and type signature as a method in its
superclass, then the method in the subclass is said to
override the method in the superclass. When an
overridden method is called from within a subclass, it
will always refer to the version of that method
defined by the subclass. The version in the superclass
will be hidden. If you wish to access the superclass’
version of an overridden method, you can do so by
for any particular method to override another
return type, method name, type and order of
arguments must be identical to those of a method in
a parent class.
accessibility must not be more restrictive than
The method must not throw checked exceptions of
classes that are not possible for the original method.
Whenever a subclass needs to refer to
its immediate superclass, it can do so by use of the
keyword ‘super’. This
keyword has two general forms. The first calls the
superclass constructor. The second is used to access a
member of the superclass that has been hidden by a
member of a subclass.
Constructor call: The call to the
superclass’ constructor must always be the first
statement executed inside a subclass’ constructor.
The second form of ‘super’ acts like ‘this’, except that it always
refers to the superclass of the subclass in which it
is used (e.g. super.member). This form of super is
most applicable to situations in which member names
of a subclass hide members by the same name in the
In a class hierarchy, constructors are called in order
of derivation from superclass to subclass. Further,
since super () must be the first statement executed in
a subclass’ constructor, this order is the same
whether or not super () is used. If super () is not
used, then the default or no-arguments constructor of
each superclass will be executed. This is because a
superclass has no knowledge of any subclass, any
initialization it needs to perform is separate from
and possibly prerequisite to any initialization
performed by the subclass. Therefore it must be
These behave as
section6-1 | section6-2 | section6-3
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11