Method Overriding Rules in Java

Upasana | May 26, 2019 | 3 min read | 1,562 views


A subclass can override instance methods that it inherits from its superclass. Overriding such a method allows the subclass to provide its own implementation of the method. The below rules must be obeyed while overriding such a method in subclass:

  • The overriding method in subclass should have exactly same method signature as that of overridden method in superclass. Method signature includes - method name, type and number of parameters and the order of arguments. If they don’t then it will result in overloading rather than overriding.
    Method signature does not comprise the final modifier of the parameter

  • The return type of overriding method must be a covariant type (same class or sub-class), i.e. you can narrow down the return type.

  • The overriding method cannot narrow the accessibility of the method, but it can widen it.

  • You are free to throw any kind of Runtime Exception in the overriding method (But this is not applicable for checked exception)

  • You shall only throw same or narrowed checked exception (same or sub-class) or none at all.

  • methods marked private or final can not be overridden in subclass

  • static methods can not be overridden since they are not instance methods of superclass.

  • If a method is not inherited, then it can not be overridden. For example, private methods in superclass are not inherited.

Practice 1. Test the code for overriding rules

Check if the below code correctly overrides the methods in superclass?

public class Foo {
    public void doStuff(int x, String y) {
        //Foo implementation
    }
}

public class Bar extends Foo {

    public void doStuff(int x, long y) {
        //Bar implementation
    }

}

Answer: No

The above method does not override since the type of argument is not same as that of superclass method. This is infact an overloading of method. Please be noted that a method can be overloaded in the same type or the subtype.

Practice 2. Test the below code for overriding rules

Check if the below code correctly overrides the methods in superclass?

public class Foo {
    private void doStuff(int x, String y) {
        //Foo implementation
    }
}

public class Bar extends Foo {

    public void doStuff(int x, String y) {
        //Bar implementation
    }

}

Answer: No

Since private methods are not inherited, so they can not be overridden by subclass method.

Practice 3.

If a method throws FileNotFoundException in a super Class, can we override this method in sub-class by throwing IOException?

Answer

FileNotFoundException extends IOException, and both of these are Checked Exceptions. So As per overriding rules, sub-class method can only narrow down the scope of Exception i.e. overriding method can only throw the same Exception or sub-classes of that Exception. So overriding method can not throw IOException in this case.

Practice 4.

If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException, please note that NPE is sub-class of RuntimeException?

Answer

Yes, overriding method can throw runtimeException. There is no policy for RuntimeException (unchecked exceptions) in method overriding.

References

  1. OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808) by Kathy Sierra, Bert Bates.

  2. A Programmer’s Guide to Java SE 8 (OCA) by Khalid A. Mughal


Top articles in this category:
  1. Difference between method overloading and overriding in Java
  2. Method overloading rules in Java
  3. Multi-threading Java Interview Questions for Investment Bank
  4. Single Abstract Method (SAM) and Functional Interface in Java
  5. RBS Java Programming Interview Questions
  6. Citibank Java developer interview questions
  7. Sapient Global Market Java Interview Questions and Coding Exercise

Recommended books for interview preparation:

Find more on this topic: