Overriding in OOPs

Back to home
Logicmojo - Updated Jan 11, 2023



What is Overriding?

Whatever the Parent has by default available to the Child through inheritance, if the Child is not satisfied with Parent class method implementation then Child is allow to redefine that Parent class method in Child class in its own way this process is called overriding.

Method overriding refers to when a piece of code contains two or more methods with the same name, but each one performs a different task. Taking the literal sense of the name, this means that one approach must take precedence over another. This idea refers to redefining a base class method with the same method signature in a derived class.

In overriding method resolution is always takes care by JVM based on runtime object hence overriding is also considered as runtime polymorphism or dynamic polymorphism or late binding.

Example

class Parent 
{
	public void method1()
	{
		System.out.println("Parent Method 1");
	}
	public void method2()
	{
		System.out.println("Parent Method 2");	
	}
}
class Child extends Parent					  
{
	public void method1()
	{
		System.out.println("Child Method 1");	  
	}
}
class Main 
{
	public static void main(String[] args) 
	{
		Parent p=new Parent();
		p.method1();//parent method 1
		Child c=new Child();
		c.method1();//child method 1
		Parent p1=new Child();
		p1.method1();//child method 1
	}
}

Rules for Overriding

  1. In overriding method names and arguments must be same. That is method signature must be same.

  2. In Java, Until 1.4 version the return types must be same but from 1.5 version onwards co-variant return types are allowed.

  3. According to this Child class method return type need not be same as Parent class method return type its Child types also allowed.

  4. Parent class non final methods we can override as final in child class. We can override native methods in the child classes.

  5. We should override Parent class abstract methods in Child classes to provide implementation

  6. We can override Parent class non abstract method as abstract to stop availability of Parent class method implementation to the Child classes.

  7. While overriding we can’t reduce the scope of access modifier.

  8. Synchronized, strictfp, modifiers won’t keep any restrictions on overriding.

A real example of Java Method Overriding

Consider the case where Bank is a class that provides functionality for calculating interest rates. The interest rate, on the other hand, differs between banks. SBI, ICICI, and AXIS banks, for example, might provide interest rates of 8%, 7%, and 9%, respectively.


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


Advantages of Overriding

  1. Method Overriding is used to provide specific implementation of a method that is already provided by its super class.

  2. Helps in writing generic code based on parent class or interface as object resolution happens at runtime

  3. Defines what behavior a class can have and implementation of behavior has been taken care by class which is going to implement.

  4. Provides multiple implementation of same method and can invoke parent class overridden method using super keyword

  5. Method Overriding is used for Runtime Polymorphism

With this article at Logicmojo, you must have the complete idea of Overriding in OOPs