Java - Interview Questions and Answers on Overloading and Overridding

Q1.  Can we reduce the visibility of the overridden method ?

Ans. No

Q2.  Does java allow overriding static methods ?

Ans. No. Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior. 

Q3.  What restrictions are placed on method overriding?

Ans. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.

Q4.  Difference between Overloading and Overriding ?

Ans. Overloading - Similar Signature but different definition , like function overloading. 

Overriding - Overriding the Definition of base class in the derived class.

Q5.  What is a Final Method ?

Ans. A Method that cannot be overriden in the sub class.

Q6.  Can we reduce the visibility of the inherited or overridden method ?

Ans. No.

Q7.  Can we overload constructors ?

Ans. Yes.

Q8.  What are points to consider in terms of access modifier when we are overriding any method?

Ans. 1. Overriding method can not be more restrictive than the overridden method.

reason : in case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.

If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime—when the JVM invokes the true object's version of the method rather than the reference type's version then it will be problematic

2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.

3. We can not override any private method because private methods can not be inherited and if method can not be inherited then method can not be overridden.

Q9.  What is covariant return type? 

Ans. co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass. it has been introduced since jdk 1.5

Q10.  How compiler handles the exceptions in overriding ?

Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception

3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method