Java - Interview Questions and Answers on Polymorphism

Q1.  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.

Q2.  Can we override static methods ? Why ?

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. 

A static method is not associated with any instance of a class so the concept of overriding for runtime polymorphism using static methods is not applicable.

Q3.  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.

Q4.  What will be the output of following code ?

class BuggyBread1 {
     public String method() {
          return "Base Class - BuggyBread1";
     }
}

class BuggyBread2 extends BuggyBread1{ 
  
     private static int counter = 0;

     public String method(int x) {
         return "Derived Class - BuggyBread2";
     }
      
     public static void main(String[] args) {
          BuggyBread1 bg = new BuggyBread2();  
          System.out.println(bg.method());
     } 
}


Ans. Base Class - BuggyBread1

Though Base Class handler is having the object of Derived Class but its not overriding as now with a definition having an argument ,derived class will have both method () and method (int) and hence its overloading.

Q5.  Is this Polymorphism ?

Map<String, List<String>> inventoryManagerCountMap = new HashMap<String, ArrayList<String>>();

Ans. No. This will result in compilation error as Polymorphism cannot be performed on Object types.  
Q6.  Which of the following is not the difference between Singleton and Static class ( Class with static members only ) ?

 a. Only one object can be created for Singleton class whereas No objects are created for static class. 
 b. Singleton class instance is initiated using new keyword whereas static class instance is created using static method.
 c. Singleton class can be serialized whereas Static class cannot be.
 d. Singleton Class can participate in runtime Polymorphism whereas Static class cannot.

Ans. Singleton class instance is initiated using new keyword whereas static class instance is created using static method.

Q7.  Which of the following do you think is the primary reason you would never use a static class even the application doesn't need multiple requests or threads ?

 a. Serialization
 b. Runtime Polymorphism
 c. Lazy Loading
 d. Memory 

Ans. Runtime Polymorphism

Q8.  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

Q9.  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