Overloading vs Overriding in Java.

In this blogpost we will discuss the difference between overloading and overriding in Java. If you are new to these terms then refer the following posts:

Overloading vs Overriding in Java

  1. Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.
  2. Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.
  3. The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.
  4. Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
  5. Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
  6. private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.
  7. Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type
  8. Argument list should be different while doing method overloading. Argument list should be same in method Overriding.

Following are rules of method overriding in java which must be followed while overriding any method. As stated earlier privatestatic and final method can not be overridden in Java. 
  1. Method signature must be same including return type, number of method parameters, type of parameters and order of parameters 
  2. Overriding method can not throw higher Exception than original or overridden method. means if original method throws IOException than overriding method can not throw super class of IOException e.g. Exception but it can throw any sub class of IOException or simply does not throw any Exception. This rule only applies to checked Exception in Java, overridden method is free to throw any unchecked Exception
  3. Overriding method can not reduce accessibility of overridden method , means if original or overridden method is public than overriding method can not make it protected. 

Overloading Example

public class Div {
    public int div(int a, int b) {
        return (a / b);
    }
    public int div(int a, int b, int c) {
        return ((a + b) / c);
    }

    public static void main(String args[]) {
        Div ob = new Div();
        ob.div(10, 2);
        ob.div(10, 2, 3);
    }
}

Overrriding Example code:

public class Test {
    public static int func(int a) {
        return 100;
    }
    public static char func(int a, int b) {
        return "java booth";
    }
    public static void main(String args[]) {
        System.out.println(func(1));
        System.out.println(func(1, 3));
    }
}