Method Overriding in Java

Hello, readers. In today’s topic, we’ll discuss method overriding in java. These fundamental concepts must be understood before creating a Java program. In case you missed it, we also discussed decision-making, loop control, and Java methods before these. Hurry up now! A number of other Java-related topics, such as Operator in Java, Data Types in Java, Difference between C++ & Java, and many others, have also been briefly covered in the past. Your understanding will greatly benefit from this information. So let’s get to the point without any further ado:

Method Overriding in Java

Method overriding is a term used in Java when a subclass (child class) has a method that is also declared in the parent class. A method used by Java to achieve Run Time Polymorphism is method overriding.
Simply, in any object-oriented programming language, overriding is a function that calls for a subclass or child class to provide a wide range of method implementations that are already provided by one of its superclasses (parent classes). So when a method is called from an object of a parent class, the method’s parent class version is executed; however, when a method is called from an object of a subclass, the child class version is executed.

Let’s use a clear example to illustrate this. We have two classes: the parent class Persons and the child class GeektoCode. Persons class is extended by GeektoCode class. Both classes share the void sleep method (). The sleep() method is being overridden by the GeektoCode class, which provides its own implementation.

Here, the goal of Method Overriding is evident. In order to print GeektoCode is sleeping rather than Rahul is sleeping when this method is called, the child class wants to provide its own implementation.

Example:-

// Example of Method Overloading in Java by GeektoCode
class Persons{
     public void sleep() //Overridden method
     {
          System.out.println("Rahul is sleeping");
     }
}
public class GeektoCode extends Persons {
     @Override
     public void sleep() //Overriding method
     {
         System.out.println("GeektoCode is sleeping");
     }
     public static void main(String args[]){
         GeektoCode obj = new GeektoCode();
         obj.sleep(); //This will call the child class version of eat()
     }
}

OUTPUT:-

GeektoCode is sleeping

Here both the Persons superclass and the GeektoCode subclass contain the sleep() method in the program above.
The method contained within the subclass GeektoCode is called when sleep() is called using the obj object (object of the subclass). The superclass’s equivalent method, sleep(), is overridden by the subclass’s sleep() method.
Take note of how our example makes use of the @Override annotation. Annotations are the metadata we use in Java to give the compiler information. The compiler is informed by the @Override annotation in this case that the method that follows this annotation replaces the method of the superclass.
@Override is not required to be used. To use this, the method must adhere to all overriding requirements. Otherwise, a compiler error will be produced here.

Rules for Java Overriding

  • The method name, return type, and parameter list must all be the same for the superclass and the subclass.
  • The method that has been declared final and static cannot be overridden.
  • Overriding the superclass’s abstract methods is a must (which will be discussed in later tutorials).

Super Keyword in Java Overriding

Frequently asked questions about overriding in Java include: After overriding, is it possible to access the superclass method?
The answer is yes. We use the super keyword to call a method on the superclass from a subclass.

Example:-

// Example of Method Overriding in Java by GeektoCode
class Persons {
     public void sleep() {
          System.out.println("Rahul is sleeping");
     }
}
class Boy extends Persons {
     public void sleep() {
          super.sleep();
          System.out.println("Boy is sleeping.");
     }
}
class GeektoCode {
     public static void main(String[] args) {
          Boy obj = new Boy();
          obj.sleep();
     }
}

OUTPUT:-

Rahul is sleeping
Boy is sleeping.

In the above-mentioned illustration, the method sleep() of the superclass Persons is overridden by the subclass Boy. The method inside the Boy subclass is called when the method sleep() is called using the obj object of the Boy subclass; the method inside the superclass is not called. We have called sleep() of the superclass inside of the Boy subclass’s sleep() method using super.sleep().

Advantages of overriding methods

The main benefit of method overriding is that it allows a class to customize an inherited method without even altering the code of the parent class.
When a class has several child classes, this is advantageous because it allows any child class that needs to use a parent class method to do so, while allowing other classes that want to use a different implementation to do so by using the overriding feature without changing the parent class code.

Thus, Java’s method overriding is the main topic here. I hope you find this useful. Wait for this to meet in the next post, where we will discuss a brand-new aspect of Java. Thanks a lot.

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry