Monday , September 25 2017
Home / java / Overriding vs Overloading in Java

Overriding vs Overloading in Java

Overriding vs overloading is regarded as one of the famous interview questions asked from Java at numerous companies and by plenty of programmers. There are some significant differences taking place between method overriding and overloading in Java. Although, it is extremely essential to know how to employ both overriding and overloading concept in Java since these differentiation are highly useful when considering for interview point of view and hence provide you some basic idea.

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

Method Overloading In Java

If you have two methods with similar name in single Java class with dissimilar method signature then it is known as overloaded method. Usually, Java overloaded method have different set of arguments to carry out something depending upon dissimilar number of input.

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

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

Example


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:

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

Method Overriding In Java

So as to do method override in Java, you ought to create a baby class from parent. Since, the method overridden in Java will shares similar name alike the original method however can be overridden only in the sub-class. Furthermore, the original method has to define within the base class or interface which seems to be abstract also.

Example


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:

Child Method
parent.method2() : Child Method2.

When you tend to do method override in Java, its signature stays exactly similar consisting of return type. Another notable point in Java overriding is that you cannot able to override static method since it is connected with class instead of object as well as determined and bonded at the time of compilation so you cannot able to override main method. Like static, final and private method cannot be overridden in Java.

 Overloading vs Overriding

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

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *