Method Overloading vs Method Overriding

Method overloading and overriding ( in other words, polymorphism in java) is neither a very difficult concept and nor it’s one of very unknown topics. Yet, I am bringing this topic here in this post, because at the same time it is very easy to make mistakes when such concepts are tested in java interviews using multiple code examples. I am not giving any new concept here, but I intend to revise your existing knowledge regarding rules of method overloading and overriding in java.

Method Overloading Rules

Here are the rules which you keep in mind while overloading any method in java:

1) First and important rule to overload a method in java is to change method signature. Method signature is made of number of arguments, type of arguments and order of arguments if they are of different types.
public class DemoClass {
    // Overloaded method
    public Integer sum(Integer a, Integer b) {
        return a + b;
    }
 
    // Overloading method
    public Integer sum(Float a, Integer b) {  //Valid
        return null;
    }
}
 
2) Return type of method is never part of method signature, so only changing the return type of method does not amount to method overloading.
public class DemoClass {
    // Overloaded method
    public Integer sum(Integer a, Integer b) {
        return a + b;
    }
 
    // Overloading method
    public Float sum(Integer a, Integer b) {     //Not valid; Compile time error
        return null;
    }
}
 
3) Thrown exceptions from methods are also not considered when overloading a method. So your overloaded method throws the same exception, a different exception or it simply does no throw any exception; no effect at all on method loading.
public class DemoClass {
    // Overloaded method
    public Integer sum(Integer a, Integer b) throws NullPointerException{
        return a + b;
    }
 
    // Overloading method
    public Integer sum(Integer a, Integer b) throws Exception{  //Not valid; Compile time error
        return null;
    }
}

Method Overriding Rules

We read above the rules for method overloading, now its time to list down the rules which you should keep remember while overriding a method in java.

1) The method argument list in overridden and overriding methods must be exactly same If they don’t match, you will end up with an overloaded method.

2) The return type of overriding method can be child class of return type declared in overridden method.
public class SuperClass {
    //Overriden method
    public Number sum(Integer a, Integer b) {
        return a + b;
    }
}
 
class SubClass extends SuperClass {
    //Overriding method
    @Override
    public Integer sum(Integer a, Integer b) {      //Integer extends Number; so it's valid
        return a + b;
    }
}
 
3) Above all rules, private, static and final methods can not be overridden in java in any way. As simple as that !!
public class SuperClass {
    private Integer sum(Integer a, Integer b) {   //private method; overriding not possible
        return a + b;
    }
}
 
class SubClass extends SuperClass {
    //Overriding method
    public Integer sum(Integer a, Integer b) {  
        return a + b;
    }
}
 
4) Overriding method can not throw checked Exception higher in hierarchy than thrown by overridden method. Let’s say for example overridden method in parent class throws FileNotFoundException, the overriding method in child class can throw FileNotFoundException; but it is not allowed to throw IOException or Exception, because IOException or Exception are higher in hierarchy i.e. super classes of FileNotFoundException.
More to it, you can omit the exception declaration from overriding method. It’s allowed and perfectly valid. Also overriding method can throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
public class SuperClass {
    //Overriden method
    public Integer sum(Integer a, Integer b) throws FileNotFoundException {
        return a + b;
    }
}
 
class SubClass extends SuperClass {
    //Overriding method
    public Integer sum(Integer a, Integer b) throws IOException {       //Not valid; Compile time error
        return a + b;
    }
    //Exception IOException is not compatible with throws clause in SuperClass.sum(Integer, Integer)
    public Integer sum(Integer a, Integer b)  {                     //It's valid; Don't declare the exception at all is permitted.
        return a + b;
    }
}
 
5) Also note that overriding method can not reduce the access scope of overridden method. Put in simple words, if overridden method in parent class is protected, then overriding method in child class can not be private. It must be either protected (same access) or public (wider access).
public class SuperClass {
    //Overriden method
    protected Integer sum(Integer a, Integer b) {
        return a + b;
    }
}
 
class SubClass extends SuperClass {
    //Overriding method
    //Not valid; Compile time error "Cannot reduce the visibility of the inherited method from SuperClass"
    private Integer sum(Integer a, Integer b)  {   
        return a + b;
    }
}