14 Overriding Dynamic Dispatch
14 Overriding Dynamic Dispatch
Inheritance
1
Signatures
• In any programming language, a signature is what distinguishes one
function or method from another
• Method Signature in java is defined as the structure of a method that
is designed by the programmer.
• Method Signature is the combination of a method's name and its
parameter list
• In Java, two methods have to differ in their names or in the number or
types of their parameters
• foo(int i) and foo(int i, int j) are different
• foo(int i) and foo(int k) are the same
• foo(int i, double d) and foo(double d, int i) are different
2
Polymorphism
• Polymorphism means many (poly) shapes (morph)
• In Java, polymorphism refers to the fact that you can have multiple
methods with the same name in the same class
• There are two kinds of polymorphism:
• Overloading
• Two or more methods with different signatures
• Overriding
• Replacing an inherited method with another having the same signature
3
Overloading
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
4
Why overload a method?
• So you can use the same names for methods that do essentially the same thing
• Example: println(int), println(double), println(boolean), println(String), etc.
• So you can supply defaults for the parameters:
int increment(int amount) {
count = count + amount;
return count;
}
int increment() {
return increment(1);
}
• Notice that one method can call another of the same name
• So you can supply additional information:
void printResult() {
System.out.println("total = " + total + ", average = " + average);
}
void printResult(String message) {
System.out.println(message + ": ");
printResults();
} 5
Method Overriding
• If subclass (child class) has the same method as declared
in the parent class, it is known as method overriding in
Java
• Usage of Java Method Overriding:
• Method overriding is used to provide the specific
implementation of a method which is already provided by its
superclass.
• Method overriding is used for runtime polymorphism
6
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 its
subclass, it will always refer to the version of that method
defined by the subclass.
• The version of the method defined by the superclass will
be hidden.
7
Rules for Java Method Overriding
8
Understanding the problem without Method Overriding
//Creating a parent class
class Vehicle{
void run(){System.out.println("Vehicle is running");} Output:
Vehicle is running
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run(); Problem is that I have to provide a specific implementation of run() method
} in subclass that is why we use method overriding.
}
9
Understanding the problem without Method Overriding
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");} Output:
} Bike is running safely
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}
11
Understanding the problem without Method Overriding
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){
super.run();
System.out.println("Bike is running safely");}
}
public class override {
public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}
12
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.
13
Dynamic Method Dispatch
• A superclass reference variable can refer to a subclass object
• When a parent class reference points to the child class object then
the call to the overridden method is determined at runtime, because
during method call which method(parent class or child class) is to be
executed is determined by the type of object.
• This process in which call to the overridden method is resolved at
runtime is known as dynamic method dispatch.
14
Dynamic Method Dispatch
15
Try this
• Write a program to find the area of rectangle, area of
circle and area of square. Use hierarchical inheritance
to accomplish this. Try method overriding.
• Create a super class called “Parents” and a subclass
“Children” and subclass “Sibling”. Print the names of
parents, siblings and children. Implement using
method overriding.
16
+-----------------+ |
| Shape | |
+-----------------+ +-----------------+
| | | Circle |
+-----------------+ +-----------------+
| calculateArea() | | double radius |
+-----------------+ +-----------------+
^ | calculateArea()|
| +-----------------+
| ^
+-----------------+ |
| Rectangle | |
+-----------------+ +-----------------+
| double length | | Square |
| double width | +-----------------+
+-----------------+ | double side |
| calculateArea()| +-----------------+
+-----------------+ | calculateArea()|
+-----------------+ 17