What We Are Learn On This Post
Dynamic Polymorphism Interview Questions
In this article, we will cover some of the interview questions with their justification for Dynamic Polymorphism or method overriding. These are the most frequently asked interview questions from OOPS concepts. Before reading the interview question & answers, go through Java Method Overriding concepts
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
Read Also: Static Polymorphism Interview Questions
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)
Check Also: Method Overloading In Java
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
Check Also: JMeter Interview Questions
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
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