0% found this document useful (0 votes)
23 views17 pages

14 Overriding Dynamic Dispatch

Uploaded by

srishreyan10b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views17 pages

14 Overriding Dynamic Dispatch

Uploaded by

srishreyan10b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

JAVA Programming

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

1. The method must have the same name as in the parent


class.
2. The method must have the same parameter as in the
parent class.
• The same number and types of parameters
• Parameter names don’t matter, just their types
3. There must be an IS-A relationship (inheritance).
4. The return type must be the same

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");}

public static void main(String args[]){


Bike2 obj = new Bike2();//creating object
Here we have defined the run method in the subclass as defined in the
obj.run();//calling method parent class but it has some specific implementation. The name and
} parameter of the method are the same, and there is IS-A relationship
between the classes, so there is method overriding.
}
10
Understanding the problem without Method Overriding
• When run( ) is invoked on an object of type Bike2, the version of
run( ) defined within Bike2 is used.
• That is, the version of run( ) inside Bike2 overrides the version
declared in Vehicle.
• If you wish to access the superclass version of an overridden
method, you can do so by using super.

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

You might also like