Real example of Java Method Overriding:-
Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 5%, 6% and 7% rate of interest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Bank{ int getRateOfInterest(){return 0;} } class SBI extends Bank{ int getRateOfInterest(){return 5;} } class ICICI extends Bank{ int getRateOfInterest(){return 6;} } class AXIS extends Bank{ int getRateOfInterest(){return 7;} } class DisplayResult{ public static void main(String args[]){ SBI s = new SBI(); ICICI i = new ICICI(); AXIS a = new AXIS(); System.out.println("Rate of Interest in SBI is "+s.getRateOfInterest()+"%"); System.out.println("Rate of Interest in ICICI is "+i.getRateOfInterest()+"%"); System.out.println("Rate of Interest in AXIS is "+a.getRateOfInterest()+"%"); } } |
1 2 3 4 |
Output:- Rate of Interest in SBI is 5% Rate of Interest in ICICI is 6% Rate of Interest in AXIS is 7% |
Advantages of method overriding
- The main advantage of method overriding is that the class can give its own specific implementation to the inherited method without even modifying the parent class(base class).
- The benefit of overriding is ability to define a behavior that’s specific to the subclass type which means a subclass can implement a parent class method based on its requirement.
Method Overriding in dynamic method dispatch
Dynamic method dispatch is a technique which enables us to assign the base class reference to a child class object. As you can see in the below example that the base class reference is assigned to child class object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class ABC{ public void disp() { System.out.println("disp() method of parent class"); } public void abc() { System.out.println("abc() method of parent class"); } } class Test extends ABC{ public void disp(){ System.out.println("disp() method of Child class"); } public void xyz(){ System.out.println("xyz() method of Child class"); } public static void main(String args[]){ //Parent class referenced to child class object ABC obj = new Test(); obj.disp(); obj.abc(); } } |
1 2 3 |
Output:- disp() method of Child class abc() method of parent class |
Note: In dynamic method dispatch the object can call the overriding methods of child class and all the non-overridden methods of base class but it cannot call the methods which are newly declared in the child class. In the above example the object obj was able to call the disp()(overriding method) and abc()(non-overridden method of base class).
However if you try to call the xyz () method (which has been newly declared in Test class) [obj.xyz()] then it would give compilation error with the following message:
Main.java: error: cannot find symbol
obj.xyz();
Problem: The method xyz() is undefined for the type ABC
…continued in the next article Method Overriding in Java: – PART-3