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 {
public Integer sum(Integer a, Integer b) {
return a + b;
}
public Integer sum(Float a, Integer b) {
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 {
public Integer sum(Integer a, Integer b) {
return a + b;
}
public Float sum(Integer a, Integer b) {
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 {
public Integer sum(Integer a, Integer b) throws NullPointerException{
return a + b;
}
public Integer sum(Integer a, Integer b) throws Exception{
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 {
public Number sum(Integer a, Integer b) {
return a + b;
}
}
class SubClass extends SuperClass {
@Override
public Integer sum(Integer a, Integer b) {
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) {
return a + b;
}
}
class SubClass extends SuperClass {
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 {
public Integer sum(Integer a, Integer b) throws FileNotFoundException {
return a + b;
}
}
class SubClass extends SuperClass {
public Integer sum(Integer a, Integer b) throws IOException {
return a + b;
}
public Integer sum(Integer a, Integer b) {
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 {
protected Integer sum(Integer a, Integer b) {
return a + b;
}
}
class SubClass extends SuperClass {
private Integer sum(Integer a, Integer b) {
return a + b;
}
}
Not to repeat again that method overriding is legal when talking in terms on parent classes and child classes. It does not happen within same class.