class Bank
{
int getRateOfInterest()
{
return 0;
}
}
class SBI extends Bank
{
int getRateOfInterest()
{
return 8;
}
}
class ICICI extends Bank
{
int getRateOfInterest()
{
return 7;
}
}
class AXIS extends Bank
{
int getRateOfInterest()
{
return 9;
}
}
class Test2
{
public static void main(String args[])
{
SBI s = new SBI();
ICICI i = new ICICI();
AXIS a = new AXIS();
System.out.println("SBI Rate of Interest: " + s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: " + i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: " + a.getRateOfInterest());
}
}
Step-by-Step Explanation of the Program:
Example : The following java program illustrates a method overriding. In object oriented programming, one can provide with the specific implementations of methods to be overridden declared in a superclass. Here in this example we have a Parent class called BANK and three CHILD classes SBI, ICICI, AXIS have overridden the given method getRateOfInterest
1. Definition of Classes
It has a method getRateOfInterest() that returns 0. This is the base method overridden by the subclasses.
SBI, ICICI, and AXIS Classes:
- Each class extends the Bank class.
- They override the getRateOfInterest() method to return their specific rates:
- SBI returns 8%.
- ICICI returns 7%.
- AXIS returns 9%.
2. In the Test2 Class:
- Creating Objects: An object of each subclass (SBI, ICICI, AXIS) is created.
SBI s = new SBI();
ICICI i = new ICICI();
AXIS a = new AXIS();
- Method Calls: Each object calls the getRateOfInterest() method.
Because the getRateOfInterest() method is overridden in each subclass, the appropriate subclass method is called for each object.
- s.getRateOfInterest() invokes SBI’s overridden method, which returns 8.
- i.getRateOfInterest() invokes ICICI’s overridden method which returns 7.
- a.getRateOfInterest() calls AXIS overridden method which returns 9.
Printing Output:
- The program prints the rate of interest for each bank object by invoking System.out.println().
3. Output:
When this program is run the following output will be produced.
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
Key Concepts:
- Method Overriding: In Java, a subclass can provide its own implementation of a method that is already defined in its superclass. This is called method overriding.
- The method in the subclass must have the same signature (name and parameters) as the method in the superclass.
- In this program, the getRateOfInterest() method is overridden in the SBI, ICICI, and AXIS classes to provide their specific interest rates.
- Polymorphism: The program demonstrates compile-time polymorphism (also known as method overriding), where the method that is called is determined by the type of the object at runtime.
Conclusion:
A superclass Bank is defined and three subclasses are defined (SBI, ICICI, AXIS). This last method gets overridden to return different interest rates by each subclass.
When the getRateOfInterest() method is invoked on each object, the respective subclass method gets executed.