Java – Interview Question and Answers on Method Overriding

In this article, we will cover some of the interview questions with their justification on method overriding

These are most frequently asked interview question from OOPS concepts

Before reading interview question & answers, go through Java Method Overriding concepts

Q) What is method overriding in Java ?

  • While inheriting a super class, if a sub class has a same instance method with same method signature as that of the super class’s method then it is said to be method overriding in java
  • Reason to have another exactly same method in inheriting class is to provide more specific implementation from that of more general implementation in super class

Q) Whether order of the formal parameters/inputs of overriding method in sub class can be changed ?

  • No, number of formal parameters and their order should be exactly same as that of the super class’s overridden method

Q) Can we change the access level of the overriding method in sub class ?

  • While overriding super class’s method to a sub class, access level can be kept same or it should be wider/broader (i.e.; to increase the access visibility of the overriding method in sub class)

Q) Can we reduce the visibility of the inherited or overridden method ?

  • No, we can either need to stick with the same access level or else it can widened to upper access level

Q) Whether class compiles successfully, if we decrease the access visibility of the overriding method in sub class ?

  • No, compile-time error will be thrown
  • Access level should be more wider/broader but we can’t decrease the access visibility of the overriding method in sub class
  • Access level increases in below order (with private being the least and public being the highest)
    1. private
    2. default
    3. protected
    4. public

Q) Can we change the return type of the overriding method in sub class ?

  • Return type in the overriding method can’t be changed but starting Java 5 return type should be exactly same or its sub classes (sub type)
  • This is known as co-variant return type in Java (Since JDK 1.5)

Q) What is co-variant method overriding ?

  • Covariant (wide to narrower)
  • Since Java 5, overriding method can return same or its sub-type from overriding method

Q) Can we change the exception of the overriding method in sub class ?

  • No, exception in the overriding method can’t be changed.
  • Exception thrown in the overriding method should be exactly same or its sub classes (sub type)

Q) Can we add any un-checked exception to throws clause in the overriding method in sub class, irrespective of exception thrown in overridden method in super class ?

  • Yes, we can add any un-checked exception in the overriding method irrespective of exception thrown in overriding method in super class

Q) Is it mandatory to include throws clause in the overriding method in sub class ?

  • No, we can remove throws clause from overriding method in sub class irrespective of whether super class’s overridden method throws any checked or un-checked exception

Q) Is it mandatory to throw same exception (or its sub class exception) in the overriding method in sub class ?

  • No, we can remove throws clause from overriding method in sub class irrespective of whether super class’s overridden method throws any checked or un-checked exception
  • If we are throwing some exception from overriding method in sub class then it should be exactly same or its sub-class (sub-type) exception
  • Otherwise, compile-time error will be thrown

Q) Is it possible to throw more exceptions from overriding method (more exception compared to super class’ overridden method) ?

  • Yes, we can throw more number of exceptions from overriding method in sub class as long as this is compliant with exception handling narrowing criteria
  • That is exception thrown out in the overriding method should be same or its sub-class (sub-type)
  • Any un-checked exception thrown will be valid and class compiles successfully
  • But checked exception in overriding method should maintain IS-A relationship with super class overridden method’s exception, otherwise compile-time error will be thrown

Q) Can we override private method of super class ?

  • No, private method can’t be overridden because private method can’t be inherited to sub class (Singleton design pattern)

Q) Whether protected method of super class can be overridden in sub class ?

  • Yes, protected method can be overridden as long as class is inherited (and maintains IS-A relationship)

Q) Whether static method of super class can be overridden in sub class ?

  • If super class’s method declared as static then it cannot be overridden rather it can be re-declared in inheriting class

Q) Can we override main() method (public static void main();) ?

  • No, we can’t override main() method because this is entry point for JVM to start the execution of the program and therefore it is declared static.
  • And we can’t override static method rather it can be re-declared

Q) Can we override a non-static method as static in Java ?

  • No, when we try to override a non-static method of super class as static in sub class then compiler throws error. See screen capture below for error details
  • Error: This instance method cannot override the static method from SuperClassName
12_Method_Overriding_Interview_questions_static_keyword

Q) Whether final method of super class can be overridden in sub class ?

  • No, final method cannot be overridden because final method can’t be inherited to sub class
  • And if we try to override final method in sub class then compiler throws error
  • Error: Cannot override the final method from SuperClassName
13_Method_Overriding_Interview_questions_final_keyword

Q) How to prevent method from being overridden ?

  • Using ‘final’ keyword
  • To prevent a method from overriding then add a non-access modifier ‘final’ keyword to method signature

Q) How to invoke super class’ overridden method from overriding method in sub class ?

  • We can invoke using super keyword
  • Eg.; super.overriddenMethodName();
  • Other parameters like argument lists and exception should be in compliant with super class version for successful method invocation
14_Method_Overriding_Interview_questions_super_keyword

Q) What is method hiding in Java ?

  • In method overriding concept, only instance methods are overridden whereas static method cannot overridden
  • When we have same static method in the inherited class then it is like we have re-declared same static method in sub class (exactly same signature)
  • When we re-declare the same static method in the sub class then it is said to hide the static method implementation of super class
  • Hiding parent class static method in sub class is known as method hiding in Java
  • This is resolved at the compile-time for method invocation

Q) Whether constructor overriding is possible ?

  • No, there is no concept called constructor overriding

Q) Rules and restriction for method overriding in java ?

Q) What is the difference between method overloading v/s method overriding in Java ?

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Method Overloading with example
Java Overriding - Widening and narrowing for access modifier, return type and exception handling