class Bank{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 9;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 10;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 11;}
}
class MethodOverriding{
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());
}
}
SBI Rate of Interest: 9 ICICI Rate of Interest: 10 AXIS Rate of Interest: 11 Press any key to continue . . .
The above code shows an example of method overriding in Java. There is a base class Bank that contains a method getRateOfInterest() which returns 0. Then, there are three derived classes SBI, ICICI and AXIS that inherit the Bank class and override the getRateOfInterest() method with their own implementation. The SBI class returns 9, the ICICI class returns 10 and the AXIS class returns 11.
In the main method, objects of the three derived classes are created and their getRateOfInterest() methods are called, which returns the interest rate specific to each bank. The output of the program displays the interest rate for each bank.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.