Top 32 Interview Questions on Polymorphism in Java

Here, we have listed the top 32 interview questions on Polymorphism in Java with the best possible answers.

These Java polymorphism interview questions have also covered the simple as well as tricky coding questions for the best practice so that you are able to answers of questions asked by the interviewer in any company.

So, prepare the answers of these questions before going to any technical interviews.

Java Polymorphism Interview Questions and Answers

1. What is Polymorphism in Java OOPs?

Ans: Polymorphism in java is one of the core concepts of object-oriented programming system. Polymorphism means “many forms” in Greek. That is one thing that can take many forms.

Polymorphism is a concept by which we can perform a single task in different ways. That is, when a single entity (object) behaves differently in different cases, it is called polymorphism.

In other words, if a single object shows multiple forms or multiple behaviors, it is called polymorphism.

2. What are the types of Polymorphism in Java?

Ans: There are two types of polymorphism in java. They are:

  • Static polymorphism (Compile time Polymorphism)
  • Dynamic polymorphism (Runtime Polymorphism)

3. What are different ways to achieve or implement polymorphism in Java?

Ans: Polymorphism in Java can be primarily achieved by subclassing or by implementing an interface. The subclasses can have their own unique implementation for certain features and can also share some of the functionality through inheritance.

4. How is Inheritance useful to achieve Polymorphism in Java?

Ans: Inheritance represents the parent-child relationship between two classes and polymorphism take the advantage of that relationship to add dynamic behavior in the code (or to make the program more dynamic).

5. What are the advantages of Polymorphism?
Or what is the use of polymorphism?

Ans: There are the following advantages of polymorphism in java:

a. Using polymorphism, we can achieve flexibility in our code because we can perform various operations by using methods with the same names according to requirements.

b. The main benefit of using polymorphism is when we can provide implementation to an abstract base class or an interface.

6. What are the differences between Polymorphism and Inheritance in Java?

Ans: The differences between polymorphism and inheritance in java are as follows:

a. Inheritance represents the parent-child relationship between two classes. On the other hand, polymorphism takes the advantage of that relationship to make the program more dynamic.


b. Inheritance helps in code reusability in child class by inheriting behavior from parent class. On the other hand, polymorphism enables child class to redefine already defined behavior inside parent class.

Without polymorphism, it is not possible for a child class to execute its own behavior.

7. What is Compile time polymorphism (Static polymorphism)?

Ans: A polymorphism where object binding with methods happens at compile time is called static polymorphism or compile-time polymorphism.

In static polymorphism, the behavior of method is decided at compile-time based on the parameters or arguments of method.

8. How to achieve or implement static polymorphism in Java?

Ans: Static polymorphism can be achieved by method overloading. Other examples of compile time polymorphism are constructor overloading and method hiding.

9. What is the output of the following program if no error?

class A {	
 void sum(int x, int y){
  System.out.println("Sum of two numbers: " +(x+y));	 
 }
 void sum(int x, int y, int z){
  System.out.println("Sum of three numbers: " +(x+y+z));	 
 }
public static void main(String[] args){
 A a = new A();
  a.sum(20, 30);
  a.sum(30, 40, 50);
 }
}

Ans: The output is:

Sum of two numbers: 50
Sum of three numbers: 120

10. Identify the errors in the following code.

class A {	
 void sum(int x, int y){
  System.out.println("Sum of two numbers: " +(x+y));	 
 }
 void sum(int y, int x){
  System.out.println("Sum of three numbers: " +(x+y));	 
 }
public static void main(String[] args){
 A a = new A();
  a.sum(20, 30);
 }
}

Ans: Duplicate method error.

11. What is the output of the following program if no errors?

class A {	
void m1(String x){
 System.out.println("One");	
}
protected void m1(String x, String y){
 System.out.println("Two");	
}
public static void main(String[] args){
 A a = new A();
 a.m1("ABC");
 a.m1("PQR", "XYZ");
 }
}

Ans: The output is One, Two.


12. How Java compiler differentiate between methods in Compile time Polymorphism?

Ans: During compilation, Java compiler differentiates multiple methods having the same name by their signatures.

13. Is there any errors in the following code? Will the code compile successfully?

class A {	
String m1(String x){
 System.out.println("One");
 return "ABC";
}
String m1(String y){
 System.out.println("Two");	
 return "PQR";
}
public static void main(String[] args){
 A a = new A();
 a.m1("ABC");
 }
}

Ans: Duplicate method error in the above code but the code will be compiled successfully. The output is One.

14. What is Runtime Polymorphism (Dynamic Polymorphism)?

Ans: A polymorphism where object binding with methods happens at runtime is called runtime polymorphism. In runtime polymorphism, the behavior of a method is decided at runtime.

JVM (Java Virtual Machine) binds the method call with method definition/body at runtime and invokes the relevant method during runtime when the method is called. This happens because objects are created at runtime and the method is called using an object of the class.

15. How to achieve/implement dynamic polymorphism in Java?

Ans: Dynamic or runtime polymorphism can be achieved or implemented via method overriding in java. It is another way to implement polymorphism and a common approach when we have an IS-A relationship.

16. Is it possible to implement runtime polymorphism by data members in Java?

Ans: No, we cannot implement runtime polymorphism by data members in java.

17. Will the code compile successfully? If yes, what will be the output of the program?

class A {
 void m1(A a){
  System.out.println("m1 method in class A");	 
 }
}
class B extends A {	
 public void m1(A a){
  System.out.println("m1 method in class B");
 }
}
public class Test{
public static void main(String[] args){
 A a = new A();
 a.m1(a);
 a.m1(new B());
 
B b = new B();
  b.m1(null);
 
 a = b;
 a.m1(null);
 a.m1(new A());
  }
}

Ans: Yes, the code will be compiled successfully. The output is given below:

m1 method in class A
m1 method in class A
m1 method in class B
m1 method in class B
m1 method in class B

17. What is the output of the following program if no errors?

class A {
 void m1(String x){
  System.out.println("One");	 
 }
}
class B extends A {	
 public void m1(String x){
  System.out.println("Two");
  super.m1(null);
 }
}
public class Test{
public static void main(String[] args){
 A a = new B();
 a.m1(null);
  }
}

Ans: The output is Two, One.

18. Identify the errors in the following code.

class A {
 void m1(String x){
  System.out.println("One");	 
 }
}
class B extends A {	

}
public class Test{
public static void main(String[] args){
 A a = new B();
 a.m1(new A());
  }
}

Ans: The method m1(String) in type A is not applicable for the arguments (A).

19. What is the output of the below program if no errors?

class A {
 void m1(Object obj){
  System.out.println("One");	 
 }
}
class B extends A {	
 void m1(Object obj){
  super.m1(null);
  System.out.println("Two");
 }
 void m2(Object obj){
  System.out.println("Three");	 
  this.m1(null);
 }
}
public class Test{
public static void main(String[] args){
 A a = new B();
 a.m1(new A());

B b = new B();
 b.m2(new B());
  }
}

Ans: No error. The above code will be compiled successfully. The output is given below:

One
Two
Three
One
Two

20. What are the differences between compile-time polymorphism and runtime polymorphism in java?

Ans: There are three main differences between compile-time polymorphism and runtime polymorphism that are as follows:

a) In the compile-time polymorphism, the behavior of a method is decided at compile-time. Hence, Java compiler binds method calls with method definition/body during compilation.

In runtime polymorphism, the behavior of a method is decided at runtime, JVM binds the method call with method definition at runtime and invokes the relevant method during runtime when the method is called.

b) Compile time polymorphism is also known as early binding because the binding is performed at compile time.

Runtime polymorphism is also known as late binding because the binding is performed at runtime.

c) Compile time polymorphism can be achieved via method overloading.

Runtime polymorphism can be achieved via method overriding.

21. What is Binding in Java?

Ans: The connecting (linking) between a method call and method definition is called binding in java.

22. What are the types of binding in Java?

Ans: There are two types of binding in java. They are as follows:

a. Static Binding (also known as Early Binding).
b. Dynamic Binding (also known as Late Binding).

23. What is Static binding in Java?

Ans: The binding that happens during compilation is called static binding in java. This binding is resolved at the compiled time by the compiler.

24. How Java compiler performs static binding?

Ans: Java compiler just checks which method is going to be called through reference variable and method definition exists or not.

It does not check the type of object to which a particular reference variable is pointing to it.

25. Why static binding is also called early binding in Java?

Ans: Static binding is also called early binding because it takes place before the program actually runs.

26. Give an example of static binding.

Ans: An example of static binding is method overloading.

27. What is Dynamic binding in Java?

Ans: The binding which occurs during runtime is called dynamic binding in java. This binding is resolved based on the type of object at runtime.

28. How JVM performs dynamic binding in Java?

Ans: In the dynamic binding, the actual object is used for binding at runtime. JVM resolved the method calls based on the type of object at runtime. The type of object cannot be determined by the compiler.

29. Why Dynamic binding is also called late binding in java?

Ans: Dynamic binding is also called late binding or runtime binding because binding occurs during runtime.

30. Give an example of dynamic binding in Java.

Ans: An example of dynamic binding is method overriding.

31. What is the difference between static binding and dynamic binding in Java?

Ans: Refer to this tutorial: Polymorphism in Java OOPs with Example

32. Why binding of private, static, and final methods are always static binding in Java?

Ans: Static binding is better performance-wise because java compiler knows that all such methods cannot be overridden and will always be accessed by object reference variable.

Hence, the compiler doesn’t have any difficulty in binding between a method call and method definition. That’s why binding for such methods is always static.


Recommended Interview Questions

1. 40 Java Abstract Class Interview Questions Answers

2. 50 Java Interface Interview Programming Questions

3. Top 32 OOPs Interview Questions in Java with Answers

4. Top 50 Java Inheritance Interview Questions


Hope that this tutorial has covered almost all the important interview questions on polymorphism in java with exact answers. I hope that you will have understood the level of questions asked by the interviewer in the company.
Thanks for reading!!!