Home Java Programming Language / Programs / Method Overriding in Java Example 2
🚀 Programming Example

Method Overriding in Java Example 2

👁 16,135 Views
💻 Practical Program
📘 Step Learning
Method Overriding in Java Example

💻 Program Code

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

🖥 Program Output

SBI Rate of Interest: 9
ICICI Rate of Interest: 10
AXIS Rate of Interest: 11
Press any key to continue . . .
                            

📘 Explanation

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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.