AppDividend
Latest Code Tutorials

Method Overriding in Java Tutorial | Java Method Overriding Example

0

Method Overriding in Java Tutorial | Java Method Overriding Example is today’s topic. Method overriding in java can be defined as a method in a subclass or child class which is already present in the superclass. That means the method of the subclass is having the same name, same parameter, and the same return type as the method of its superclass. The method of the superclass is known as the overridden method, whereas the subclass method is class as the overriding method. We have already seen the method overloading in this blog.

Method Overriding in Java

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 super-classes or parent classes. When a method in the subclass has the same name, same parameters or signature and same return type as the method in its super-class, then a method in the subclass is said to override a method in the super-class.

Method overriding is one of the ways by which the java achieve a Run Time Polymorphism. A version of the method that is executed will be determined by the object that is 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 the object being referred to 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 vehicle is a superclass, whereas Car and Bike are the subclasses or child class. Method of the superclass with name getspeed( ) present in both the subclass and that is called as method overrides. The word overriding only says that override something already present.  You can change the content inside of 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 to the object of the superclass on the invocation of the same method on them.
  2. It is mainly used for runtime Polymorphism.

#Rules for method overriding in Java

  1. The method name of 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 which is not shareable to the other.

#Constructor cannot be overridden

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

#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 overrides method bound to an object at Runtime.

#Method that can be overridden 

#An abstract method can be overridden  

For creating an abstract method, the class must be of an abstract type.

#Overriding vs. Overloading

 

Overriding vs. Overloading

Finally, the Java Method Overriding Example is over.

Leave A Reply

Your email address will not be published.

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