METHOD OVERRIDING IN JAVA

Method Overriding in Java is the mechanism in which both the superclass and subclass have the same method and subclass class method overrides the superclass method. It is a process of Overriding base class method by derived class method with more specific definition.

Table of Contents

[hide]
  • What is Method Overriding in Java
  • Need for Method Overriding
  • Java Method Overriding Rules

What is Method Overriding in Java

Method Overloading and Method Overrinding in Java are one of the most hot topics of the interview. Kindly learn them by heart. Most of the time difference between Overloading and Overriding in Java is asked in the interviews with examples.

Method Overriding in Java is achieved when the child class has a method that has the same name, same parameters and the same return type (or covariate return type) as the parent class method, then child method has overridden the parent class method.

Method overriding only works if there is a relationship(is-a) between two classes. This says class must be inherited. In other words, the inheritance relationship is performed between two classes.

Method overriding is often called runtime polymorphism, since JVM determines the calling method during runtime.

Example : Method Overriding in Java

copyimage
//Java Program to demonstrate the use of method Overriding in Java
// parent class
class Employee{   
	//Overriden method
	public void expenditure(){
	System.out.println("Employee Expenditure is more");
	}
}
//child class
public class Student extends Employee {
	//Overriding method
	public void expenditure(){
	System.out.println("Student Expenditure is less");
	}
	
        public static void main(String[] args){
    	Student obj=new Student();
        obj.expenditure();
    }
}  

OUTPUT :

Student Expenditure is less

The Student class extends Employee class. Both the classes have a common method void expenditure(). Student class is giving its own implementation to the expenditure() method or in other words it is overriding the expenditure() method.

overriding in java

In the above example we can say that there is method overriding since the name and parameter of the method are same, and there is IS-A relationship between classes.

Need for Method Overriding

The overriding method in Java is used to allow for the specific implementation of a method already supported by its superclass. Method overriding is used to achieve runtime polymorphism, since JVM determines the calling method during runtime.

It is useful when a class has multiple child classes, and if a child class wants to use the parent class method, it can use it, and the other classes that want different implementation can use overriding features to make improvements without touching the parent code.

Let us consider an example to see why wee need method overriding.

Example : Need for Method Overriding in Java

copyimage
//Java Program to demonstrate the need of method Overriding in Java
//parent class
class Employee{   
   //Overriden method
   public void expenditure(){
      System.out.println("Employee Expenditure is more");
   }
}
//child class
public class Student extends Employee {
   public static void main(String[] args){
      Student obj=new Student();
      obj.expenditure();
   }
}  

OUTPUT :

Employee Expenditure is more

In the above program there is a method in parent class, but for the child class or subclass we need to provide a specific implementation of expenditure() method, that is why we use method overriding.

Java Method Overriding Rules

There are certain rules which we have to follow for method overriding, lets discuss few of them : -

  • 1. Overriding and Access-Modifiers.
  • 2. Final methods can not be overridden.
  • 3. Static methods can not be overridden.
  • 4. Private methods can not be overridden.
  • 5. The overriding method must have same return type (or subtype).
  • 6. We can not override constructor as parent and child class can never have constructor with same name.

Overriding and Access-Modifiers

With an overriding method the access modifier may require more, but not less, access than the overridden method. A protected instance method in the super class , for example, may be made public, but not private in the subclass. Doing so would create an error in compile-time.

Example : Overriding and Access-Modifiers

copyimage
//Java Program to demonstrate the rules for method overriding
//Base class
class Employee {
  public void show() {
      System.out.println("Super Class method is executed");
  }
}

//Derived class 
class GraduateStudent extends Employee {
  protected void show() {
      System.out.println("Sub Class method is executed");
  }
}
//Driver class 
public class Student {
  public static void main(String args[])
  {
      Employee parentObject = new Employee(); 
      parentObject.show(); 

      GraduateStudent childObject = new GraduateStudent(); 
      childObject.show(); 
  }
}

OUTPUT :

Complile Time : Cannot reduce the visibility of the inherited method from Employee

Lets have an example of the sam program with the valid scenario :

Example : Overriding and Access-Modifiers

copyimage
//Java Program to demonstrate the rules for method overriding
//Base class
class Employee {
  //Access modifier is protected
  protected void show() {
      System.out.println("Super Class method is executed");
  }
}

//Derived class 
class GraduateStudent extends Employee {
  
  //Below method overrides the Parent show() method 
  //Access modifier public is less restrictive than protected 
  @Override
  public void show() {
      System.out.println("Sub Class method is executed");
  }
}
//Driver class 
public class Student {
  public static void main(String args[])
  {
      Employee parentObject = new Employee(); 
      parentObject.show(); 

      GraduateStudent childObject = new GraduateStudent(); 
      childObject.show(); 
  }
}

OUTPUT :

Super Class method is executedSub Class method is executed 

Final methods can not be overridden

If we dont want our method to be overridden, then use declare the method as final. We will discuss about final keyword in later modules.

Example : Overriding and Access-Modifiers

copyimage
class Employee { 
    // Can't be overridden 
    final void display() {} 
} 
  
class Student extends Employee { 
    // This would produce error 
    void display() {} 
} 

OUTPUT :

Compile Time : Cannot override the final method from Employee

Static methods can not be overridden

When you describe a static method in base class with the same signature as a static method, it's known as hiding method.

Private methods can not be overridden.

Private method cannot be overridden, since they are watched at compile time, therfore we can not override private methods in a sub class.

The overriding method must have same return type (or subtype)

We have already seen method overriding example at the starting of the module. Kindly refer the same.

As a begineer Kindly follow the tutorials from starting if you want to get the most of this topic of method overriding.