Method Overriding Interview Questions

Dynamic Polymorphism Or Method Overriding Interview Questions: Dynamic Polymorphism or Method Overriding is another fundamental concept in object-oriented programming in Java. It involves providing a specific implementation of a method in a subclass that is already defined in a parent class. Dynamic Polymorphism or Method Overriding Interview Questions are a critical part of the Java developer interview process, aimed at assessing a candidate’s knowledge and proficiency in using Method Overriding in Java, including its syntax, advantages, and best practices.

These interview questions require a candidate to demonstrate their expertise in creating and implementing overridden methods to provide specific behavior in a subclass. In this context, this article provides some commonly asked Dynamic Polymorphism or Method Overriding Interview Questions in Java programming to help Java developers prepare for interviews and have a better understanding of Dynamic Polymorphism or Method Overriding in Java.

Method Overriding Interview Questions

  • How do we implement method overriding in Java?
  • Are we allowed to override a static method in Java?
  • Why Java does not allow overriding a static method?
  • Is it allowed to override an overloaded method?
  • What is the difference between method overloading and method overriding in Java?
  • Does Java allow virtual functions?
  • What is meant by covariant return type in Java?

Method Overriding Interview Questions And Answers

Q) What is method overriding in Java?

  • While inheriting a superclass, if a subclass has the same instance method with the 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 a superclass

Q) Whether the order of the formal parameters (inputs) of an overriding method in a subclass can be changed?
Answer:
No, number of formal parameters and their order should be exactly the same as that of the super class’s overridden method

Q) Can we change the access level of the overriding method in a subclass?
Answer:
While overriding super class’s method to a subclass, access level can be kept same or it should be wider/broader (i.e.; to increase the access visibility of the overriding method in a subclass)

Q) Can we reduce the visibility of the inherited or overridden method?
Answer:
No, we can either need to stick with the same access level or else it can widen to upper access level

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

  • No, a compile-time error will be thrown
  • Access level should be wider/broader but we can’t decrease the access visibility of the overriding method in a subclass
  • Access level increases in below order (with private being the least and public being the highest)
    private–> default –> protected –> public

Q) Can we change the return type of the overriding method in a subclass?

  • Return type in the overriding method can’t be changed but starting Java 5 return type should be exactly the same or its subclasses (subtype)
  • This is known as the covariant return type in Java (Since JDK 1.5)

Q) What is the co-variant method overriding?

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

Q) Can we change the exception of the overriding method in a subclass?

  • No, an exception to the overriding method can’t be changed.
  • Exception thrown in the overriding method should be exactly the same or its subclasses (subtype)

Q) Can we add any un-checked exception to throws clause in the overriding method in a subclass, irrespective of exception thrown in an overridden method in a superclass?
Answer:
Yes, we can add any un-checked exception in the overriding method irrespective of exception thrown in an overriding method in a superclass

Q) Is it mandatory to include throws clause in the overriding method in a subclass?
Answer:
No, we can remove throws clause from an overriding method in subclass irrespective of whether super class’s overridden method throws any checked or unchecked exception

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

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

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

  • Yes, we can throw a number of exceptions from an overriding method in the subclass as long as this is compliant with exception handling narrowing criteria
  • That is an 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 a checked exception in the overriding method should maintain IS-A relationship with superclass overridden method’s exception, otherwise, a compile-time error will be thrown

Q) Can we override the private method of a superclass?
Answer:
No, the private method can’t be overridden because the private method can’t be inherited to subclass (Singleton design pattern)

Q) Whether the protected method of a superclass can be overridden in a subclass?
Answer:
Yes, the protected method can be overridden as long as a class is inherited (and maintains IS-A relationship)

Q) Whether the static method of a superclass can be overridden in a subclass?
Answer:
If super class’s method declared as static then it cannot be overridden rather it can be re-declared in inheriting a class

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

  • No, we can’t override the main() method because this is an 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?
Answer:
No, when we try to override a non-static method of a superclass as static in subclass then compiler throws an error. See screen capture below for error details

Error: This instance method cannot override the static method from SuperClassName

Q) Whether the final method of a superclass can be overridden in a subclass?

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

Q) How to prevent a method from being overridden?

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

Q) How to invoke super class’s overridden a method from an overriding method in a subclass?

  • We can invoke using super keyword
  • Eg; super.overriddenMethodName();
  • Other parameters like argument list an exception should be in compliant with superclass version for successful method invocation

Q) What is the method of hiding in Java?

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

References: Article

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

1 thought on “Method Overriding Interview Questions”

  1. Excellent blog here! Additionally your web site so much up very fast!
    What host are you using? Can I get your affiliate link to your host?

    I wish my site loaded up as quickly as yours lol

    Reply

Leave a Comment