Method Overriding in Java

What is Method Overriding in Java?

If the same method is defined in both the superclass and the subclass, then the method of the subclass class overrides the method of the superclass. This is known as method overriding. It is used for runtime polymorphism.

Example:

Code:

Java Code

class Animal {
  public void show() { // same method name
    System.out.println("I am an animal.");
  }
}

class Cat extends Animal {
  @Override // overrides the parent class -> annotation 
  public void show() { // same method name
    System.out.println("I am a cat.");
  }
}

class Main {
  public static void main(String[] args) {
    Cat c1 = new Cat();
    c1.show();
  }
}

Output: I am a cat

The above Program has show() in both parent (Animal()) and subclass (Cat()) in which the subclass overrides the parent class method.

Note: We have used annotation in our example. We may or may not use this annotation, however, we are using it to satisfy all the rules related to method overriding.

Rules for Method Overriding:

  • Both parent and child classes have the same method name.
  • There must be an IS-A relationship (inheritance).
  • Private methods can not be overridden.

Example:

Code:

Java Code

//Base class or Superclass
class Parent {
  //Access modifier of parent method is private
  // This method can not override the child class method
  private void display() {
    System.out.println("parent method is executed");
  }
}

//Derived class or Subclass
class Child extends Parent {

  //Below method can not overrides the Parent display() method 
  //This method is unique to the child class
  private void display() {
    System.out.println("child method is executed");
  }
}
//Driver class 
public class MethodOverriding3 {
  public static void main(String args[]) {
    Parent parentObject = new Parent();
    parentObject.display(); // this line when execute will throw compiler error

    Parent childObject = new Child();
    childObject.display(); // this line when execute will throw compiler error
  }
}

Output :

parent method is executed

child method is executed

Final Method can not be Overridden:

Code:

Java Code

// Simple Java program showing 
// final methods cannot be overridden 
//Parent class or Superclass  
class Parent {
  // Can't be overridden 
  final void display() {}
}
//Child class or Subclass  
class Child extends Parent {
  // This would produce compile time error 
  void display() {}
}

Output:

Static Method can not be Overridden:

You can not override a method mark static. If you define a static method in the subclass (child class) with the same method signature as the superclass(parent class) then it is called method hiding.

Code:

Java Code

//Base class or SuperClass
class Parent {

  static void display() {
    System.out.println("parent static method is executed");
  }

  void show() {
    System.out.println("parent non-static (instance) method is executed");
  }
}

//Derived class or SubClass
class Child extends Parent {

  //This method hides the Parent display() method 
  static void display() {
    System.out.println("child static method is executed");
  }
  // This method overrides the Parent show() method
  void show() {
    System.out.println("child non-static (instance) method is executed");
  }
}

//Driver Class
public class MethodOverriding4 {
  public static void main(String args[]) {
    Parent childObject = new Child();
    // static method can not be overridden,
    // so below line calls parent display() method
    childObject.display();
    // Expected child method will run
    childObject.show();
  }
}

Constructor and Overriding :

You can not override the constructor as the constructor name of the base class and child class can never be the same. (Constructor name is always the same as the class name).

Advantages of Using @Override Annotation in the Code

@Override annotation informs the compiler that the element is meant to override an element declared in a superclass.

  @Override 
  int overriddenMethod() { }

Note: It is not compulsory to use but it helps to prevent errors.

Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article