Method Overriding in Java: The Complete Guide

0
375
Method Overriding in Java Tutorial | Java Method Overriding Example

In any object-oriented programming language, Overriding is the feature that allows a subclass or child class to provide a specific implementation of the method that is already provided by one of its superclasses or parent classes.

Method Overriding in Java

Method overriding in Java can be defined as a method in a subclass or child class already present in the superclass. That means the subclass’s method has the same name, parameter, and return type as the method of its superclass.

The superclass method is known as the overridden method, whereas the subclass method is classified as the overriding method. We have already seen the method overloading in this blog.

When a method in the subclass has the same name, parameters, or signature, and returns the type as the method in its superclass, then a method in the subclass is said to override it.

Method overriding is one of the ways by which Java achieves a Run Time Polymorphism. A version of the executed method will be determined by the object used to invoke it.

If the object of a parent class is used to invoke a method, then a version in the parent class will be executed, but if the object of the subclass is used to invoke a method, then a version in a child class will be executed.

In other words, it is a type of object referred to as the reference variable that determines which version of the overridden method will be executed.

See the following figure.

Method Overriding in Java

Explanation

The above diagram shows that the vehicle is a superclass, whereas Car and Bike are the subclasses or child class. The method of the superclass with the name getspeed( ) present in both the subclass is called method overrides.

The word overriding only says that override something already present. You can change the content inside the return, but the method name should be the same.

Let’s see a simple program of method overriding.

class Vehicle {
  String getspeed() {
    return "20 km/hr";
  }
}

class Car extends Vehicle {
  String getspeed() /* method is overrides */
  {
    return "40 km/hr";
  }
}

class Bike extends Vehicle {
  String getspeed() /* method is overrides */
  {
    return "35 km/hr";
  }
}

class override {
  public static void main(String args[]) {
    Vehicle v = new Vehicle();
    System.out.println("speed is " + v.getspeed());
    Vehicle c = new Car();
    System.out.println("speed of car " + c.getspeed());
    Vehicle b = new Bike();
    System.out.println("speed of bike " + b.getspeed());
  }
}

See the output.

Method Override

Use of method overriding

  1. When you want the object of the subclass to behave differently from the object of the superclass on invocating the same method.
  2. It is mainly used for runtime Polymorphism.

Rules for method overriding in Java

  1. The method name of the subclass and parent class must be the same.
  2. The arguments must be the same in the parent and child class.
  3. There must be an is-a relationship between parent class and child class. (inheritance).
  4. The overriding method must be of the same return type.

 Method name of subclass and parent class must be the same

See the following program.

class Vehicle {
  String speed() {
    return "20 km/hr";
  }
}

class Bike extends Vehicle {
  String speed() /* same name as in vehicle(super) class */
  {
    return "35 km/hr";
  }
}

class program_2 {
  public static void main(String args[]) {
    Vehicle v = new Vehicle();
    System.out.println("speed is " + v.speed());
    Vehicle b = new Bike();
    System.out.println("speed of bike " + b.speed());
  }
}

See the output.

Method name of subclass and parent class must be the same

The argument must be the same in parent and child class

See the following program.

class add {
  int solution(int a, int b) {
    return a + b;
  }
}

class multi extends add {
  int solution(int a, int b) /* same argument as parent class */
  {
    return a * b;
  }
}

class Demo {
  public static void main(String[] args) {
    add ob = new add();
    System.out.println("3+5 = " + ob.solution(3, 5));
    add ob1 = new multi();
    System.out.println("3*6 = " + ob1.solution(3, 6));
  }
}

See the output.

The argument must be the same in parent and child class

Must be an is-a relationship among parent and child class

See the following program.

class vehicle {
  void speed() {
    System.out.println("Drive vehicle safely");
  }
}

class car extends vehicle {
  void speed() {
    System.out.println("Drive car safely");
  }

  public static void main(String[] args) {
    car c = new car();
    c.speed();
  }
}

See the output.

Must be an is-a relationship among parent and child class

Overriding method must be of the same return type

When the return type is of the same type, it is called a covariant return type.

See the following program.

class one {
  one get() {
    return this;
  }
}

class two extends one {
  two get() {
    return this;
  }

  void show() {
    System.out.println("this is covariant return type");
  }

  public static void main(String args[]) {
    new two().get().show();
  }
}

See the output.

Overriding method must be of the same return type

Methods that cannot be overridden

A private method cannot be overridden.

This is because the keyword private is meant to be kept private or is not shareable to the other.

Constructor cannot be overridden

This is because the constructor cannot be inherited. Therefore, they are the same as a method, but the only difference is that the constructor does not follow the inheritance property, whereas the method follows.

Final method cannot be overridden

The word final means fixed or which cannot be changed later. So we cannot override a final method.

Static method cannot be overridden

The bound of the static method is within the class, whereas the overrides method is bound to an object at Runtime.

Method that can be overridden 

An abstract method can be overridden  

The class must be of an abstract type for creating an abstract method.

Overriding vs. Overloading

Overriding vs. Overloading

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.