Method Overriding Rules in Java

Carvia Tech | May 26, 2019 | 3 min read | 112 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. Citibank Java developer interview questions
  2. Morgan Stanley Java Interview Questions
  3. BlackRock Top Java Interview Questions: Investment Banking Domain
  4. Top 50 Multi-threading Java Interview Questions for Investment Bank
  5. RBS Java Interview Questions
  6. Sapient Global Market Java Interview Questions and Coding Exercise
  7. Cracking core java interviews - question bank



Find more on this topic:
Java Interviews image
Java Interviews

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info