Professional Documents
Culture Documents
Practical No 6
Practical No 6
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.
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.
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.
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 child classes.
class SBI extends Bank{
void getRateOfInterest(){
double roi=8.4;
super.getRateOfInterest();
System.out.println("SBI Rate of Interest: "+roi);
}
}
}
}
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.