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
  • The reason to have another exactly similar method in inheriting a class is to provide a more specific implementation from that of a 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, the 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 the super class’s method to a subclass, the access level can be kept the 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 the upper access level

Q) Will The class compile successfully if we decrease the access visibility of the overriding method in a subclass?

  • No, a compile-time error will be thrown
  • The 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?

  • The return type in the overriding method can’t be changed, but starting in 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 overriding of the co-variant method?

  • 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 unchecked exception to the throws clause in the overriding method in a subclass, irrespective of the 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 a throws clause in the overriding method in a subclass?
Answer: No, we can remove the throws clause from an overriding method in the subclass, irrespective of whether the 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 the throws clause from an overriding method in the subclass, irrespective of whether the super class’s overridden method throws any checked or unchecked exception
  • If we are throwing some exception from an overriding method in a 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 exceptions than 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 the same or its sub-class (sub-type)
  • Any un-checked exception thrown will be valid, and the class compiles successfully
  • However, a checked exception in the overriding method should maintain an IS-A relationship with the 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) Can the protected method of a superclass be overridden in a subclass?
Answer:
Yes, the protected method can be overridden as long as a class is inherited (and maintains an IS-A relationship)

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

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

  • No, we can’t override the main() method because it is the entry point for the JVM to start the program’s execution, and therefore, it is declared static.
  • We can’t override the 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 the subclass, the compiler throws an error. See the 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
  • If we try to override the final method in the subclass, then the compiler throws an error
  • Error: Cannot override the final method from SuperClassName

Q) How to prevent a method from being overridden?

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

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

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

Q) What is the method of hiding in Java?

  • In the method overriding concept, only instance methods are overridden, whereas static methods cannot override
  • When we have the same static method in the inherited class, then it is like we have re-declared the same static method in the subclass (exactly the 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 a method hiding in Java
  • This is resolved at the compile-time for method invocation

References: Article

Avatar for Softwaretestingo Editorial Board

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