Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Practical No 6

Aim: Implement the concept of run time polymorphism in Java.


Theory:

Polymorphism in Java is a concept by which we can perform a single action in different


ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.

Runtime Polymorphism in Java


Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an
overridden method is resolved at runtime rather than compile-time.

Method Overriding in Java

 If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.

 In other words, If a subclass provides the specific implementation of the method that
has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

o Method overriding is used to provide the specific implementation of a method which


is already provided by its superclass.

o Method overriding is used for runtime polymorphism

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.

Java Runtime Polymorphism Example: Bank

Design a Java program to model a banking system with a parent class Bank
and three child classes SBI, ICICI, and AXIS, each representing a specific bank.
The Bank class contains a method getRateOfInterest() to display the rate of
interest, which is overridden by each child class to provide the specific rate of
interest offered by that bank.
Consider a scenario where Bank is a class that provides a method to get the rate of interest.
However, the rate of interest may differ according to banks. For example, SBI, ICICI, and
AXIS banks are providing 8.4%, 7.3%, and 9.7% rate of interest.

Program:

//Creating a parent class.


class Bank{
void getRateOfInterest(){
System.out.println(" Rate of interest varies according to banks");

}
}
//Creating child classes.
class SBI extends Bank{
void getRateOfInterest(){
double roi=8.4;
super.getRateOfInterest();
System.out.println("SBI Rate of Interest: "+roi);
}
}

class ICICI extends Bank{


void getRateOfInterest(){
double roi=7.3;
System.out.println("ICICI Rate of Interest: "+roi);
}
}

class AXIS extends Bank{


void getRateOfInterest(){
double roi=9.7;
System.out.println("AXIS Rate of Interest: "+roi);
}
}

//Test class to create objects and call the methods


class overriding{
public static void main(String args[]){
SBI s=new SBI();
s.getRateOfInterest();
ICICI i=new ICICI();
i.getRateOfInterest();
AXIS a=new AXIS();
a.getRateOfInterest();

}
}
Output:

Problem Statement:

Create a Java program to showcase method overriding with a parent class Employee and
its child classes Manager and Developer. The Employee class contains a method
calculateSalary() which computes the salary of an employee. Both Manager and
Developer classes inherit from the Employee class and override the calculateSalary()
method to compute the salary specific to each role based on certain criteria.

Conclusion: Thus we learned the concept of runtime polymorphism. In this program we


achieve runtime polymorphism by using method overriding and successfully execute the
program. It is also helpful to solve real world problems.

You might also like