Overriding vs overloading is regarded as one of the famous interview questions asked from Java at numerous companies by plenty of programmers.

Overriding vs Overloading:

There are some significant differences taking place between overriding and overloading in Java. It is extremely essential to know how to employ both overriding and overloading concepts in Java. Because this differentiation is highly useful in the interview point of view. Hence we provide you with some basic ideas.

Before going to differentiate between Overriding and overloading, we should know the terms what is Overloading and Overriding.

Method Overloading In Java

A method overloading is when you have two methods with a similar name in a class with dissimilar method signatures. Usually, in Java, the overloaded method has a different set of arguments to carry out depending upon the dissimilar number of inputs.

During the compile time, binding of overloading method happens as well as overloaded calls determined using static binding. For overloading a method in Java, simply modify its signature. Keep in mind, if you wish to modify the signature, you need to modify the argument or else change the order of the argument or the type of argument if they are in dissimilar types.

Yet, if the return-type is not a division of the method signature and just changing the return type will end-up in the duplicate method as a result, you will receive a compilation error. Considering that you can overload only the static method and you can also easily overload final and private method however you cannot override them.

Example

OverloadingDemo.java
public class OverloadingDemo {
    public void meth(int value) {
        System.out.println("int value : " + value);
    }
    public void meth(String name) {
        System.out.println("String value : " + name);
    }
    public void meth(int value, String name) {
        System.out.println("Name with value : " + value+" "+name);
    }
    public static void main(String[] args) {
        OverloadingDemo demo = new OverloadingDemo();
        demo.meth(10);
        demo.meth("online tutorials point");
        demo.meth(20, "Overloading Demo");
    }
}

Output:

Terminal
int value : 10
String value : online tutorials point
Name with value : 20 Overloading Demo

Method Overriding In Java

To do method overriding in Java, you need to create a child class from the parent class. Since the overridden method in Java shares a similar name alike the original method however, we can override it only in the sub-class. Furthermore, we have to define the original method within the base class or interface which seems to be abstract also.

Example

Parent.java
class Parent {
    /**
     * Overridden Method
     */
    public void method() {
        System.out.println("Parent Method.");
    }
    /**
     * Overridden Method
     */
    public Object method2() {
        return "Parent Method2.";
    }
}
class Child extends Parent {
    /**
     * Method Overriding
     */
    public void method() {
        System.out.println("Child Method");
    }
    /**
     * Method Overriding
     */
    public String method2() {
        return "Child Method2.";
    }
}
public class OverridingDemo {
    public static void main(String[] args) {
        Child child = new Child();
        child.method();
        Parent parent = new Child();
        System.out.println("parent.method2() : " + parent.method2());
    }
}

Output:

Terminal
Child Method
parent.method2() : Child Method2.

When you tend to do method overriding in Java, its signature stays exactly similar consisting of the return type. Another notable point in overriding in java is, you cannot able to override a static method since it is connected with the class instead of an object. So you cannot be able to override the main method. Like static, final and private methods cannot be overridden in Java.

 Overloading vs Overriding

  1. In both Overriding & Overloading, method names must be the same.
  2. Methods which are having private, final and static access modifiers can be overloaded, but we cannot override those methods.
  3. There is no restriction for access modifiers for overloading. Whereas in overriding, we can’t reduce the access modifiers in child class. We can increase the scope of the access modifier.
  4. Overloading is taken care by the compiler based on the reference type, and so it is also called as compile-time polymorphism, static polymorphism or early binding. Whereas JVM based on the run time instance take care of Overriding and so it is also called as runtime polymorphism, dynamic polymorphism or late binding.

References:

Happy Learning 🙂