Method Overriding
Method Overriding
• In a 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.
LTC:Method Overriding
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
[Link]("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
[Link]("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
[Link](); // this calls show() in B
}
}
• The output produced by this program is
shown here:
k: 3
• 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 wish 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.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
[Link](); // this calls A's show()
[Link]("k: " + k);
}
}
• If you substitute this version of A into the
previous program, you will see the following
Output:
i and j: 1 2
k: 3
• Here, [Link]( ) calls the superclass
version of show( ).
• Method overriding occurs only when the
names and the type signatures of the two
methods are identical.
• If they are not, then the two methods are
simply overloaded.
Dynamic Method Dispatch
• Method overriding forms the basis for one of
Java’s most powerful concepts:
dynamic method dispatch.
• Dynamic method dispatch is the mechanism by
which a call to an overridden method is resolved
at run time, rather than compile time.
• Dynamic method dispatch is important because
this is how Java implements run-time
polymorphism.
LTC:Dynamic Method Dispatch
// Dynamic Method Dispatch
class A {
void callme() {
[Link]("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
[Link]("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
[Link]("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
[Link](); // calls A's version of callme
r = b; // r refers to a B object
[Link](); // calls B's version of callme
r = c; // r refers to a C object
[Link](); // calls C's version of callme
}
}
The output from the program is shown here:
Inside A’s callme method
Inside B’s callme method
Inside C’s callme method
• This program creates one superclass called A
and two subclasses of it, called B and C.
• Subclasses B and C override callme( ) declared
in A.
• Inside the main( ) method, objects of type A,
B, and C are declared.
• Also, a reference of type A, called r, is
declared.
• The program then in turn assigns a reference
to each type of object to r and uses that
reference to invoke callme( ).
• As the output shows, the version of callme( )
executed is determined by the type of object
being referred to at the time of the call.
Why Overridden Methods?
• As stated earlier, overridden methods allow
Java to support run-time polymorphism.
• Polymorphism is essential to object-oriented
programming for one reason: it allows a
general class to specify methods that will be
common to all of its derivatives, while allowing
subclasses to define the specific
implementation of some or all of those
methods.
Why Overridden Methods?-cont...
• Overridden methods are another way that
Java implements the “one interface, multiple
methods” aspect of polymorphism.
• Dynamic, run-time polymorphism is one of the
most powerful mechanisms that object
oriented design brings to bear on code reuse
and robustness.
• Self study: FindAreas
End of session