top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are the rules that needs to be followed, during method overloading and overriding ?

+1 vote
456 views
What are the rules that needs to be followed, during method overloading and overriding ?
posted Apr 11, 2016 by Ritika Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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;
    }
}

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.

answer Apr 15, 2016 by Karthick.c
Similar Questions
+2 votes

Weight of vowels that appear in the string should be ignored and All non-alphabetic characters in the string should be ignored.

Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26
Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.

Example1:
Let us assume the word is “Hello World!!”
Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89
Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

...