Polymorphism is an important concept in object-oriented programming languages like Java It refers to the ability of objects belonging to different data types to respond to the same method call During Java interviews, candidates are often asked polymorphism questions to test their understanding of key OOP concepts.
In this article, we will look at some of the most frequently asked polymorphism interview questions for Java developers and sample answers to help you prepare
What is Polymorphism in Java?
Polymorphism in Java allows objects of different classes to be treated as objects of a common class by the invoking application. The word “poly” means many and “morphs” means forms so polymorphism means many forms.
There are two types of polymorphism in Java:
- Compile-time polymorphism: Achieved through method overloading
- Runtime polymorphism: Achieved through method overriding
Polymorphism allows developers to write reusable and flexible code. It is considered one of the key pillars of object-oriented programming.
What is Method Overloading in Java?
You can define more than one method with the same name but different parameters. This is called method overloading. It is up to the number and type of arguments to the compiler to decide which method to call.
For example:
public class Calc { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; }}
Here, we have two add()
methods that perform the same operation but accept different data types. This allows polymorphic behavior.
What is Method Overriding in Java?
Method overriding allows a subclass to override a method present in the parent class by providing its own implementation. The signature must remain the same, only the method body is changed.
For example:
public class Parent { public void print() { System.out.println("Parent print"); }}public class Child extends Parent { @Override public void print() { System.out.println("Child print"); }}
Here, the subclass Child
provides its own implementation of the print()
method, overriding the parent’s version.
Difference between Method Overloading and Overriding
Method Overloading | Method Overriding |
---|---|
Occurs within the same class | Occurs in parent and child classes |
Must have different method signatures | Must have the same method signature |
Achieves compile-time polymorphism | Achieves runtime polymorphism |
Private, final and static methods can be overloaded | Private, final and static methods cannot be overridden |
What is a Superclass Reference Variable in Java?
A superclass reference variable in Java is a variable that refers to an instance of a subclass. It allows treating subclass objects as superclass objects.
For example:
Parent p = new Child();
Here, p
is the superclass (Parent) reference variable that points to an instance of the subclass (Child). This allows polymorphic behavior when invoking methods on p
.
Explain Dynamic Method Dispatch in Java
Dynamic method dispatch refers to the runtime determination of which method implementation to call based on the actual object, not the reference type.
For example, consider this:
Parent p = new Child();p.print();
Even though p
is of the Parent type, the Child
version of print()
will be called at runtime because the object is of the Child
type. This dynamic dispatch is made possible by method overriding.
What is a Static Method in Java? Can it be Overridden?
A static method in Java belongs to the class, not individual objects. It cannot be overridden in subclasses since method overriding is based on dynamic dispatch at runtime.
For example:
public class Parent { public static void print() { System.out.println("Parent print"); }}public class Child extends Parent { public static void print() { System.out.println("Child print"); }}
Calling Child.print()
will invoke the method on Child
whereas Parent.print()
will invoke the method on Parent
. There is no polymorphism here.
What is a Final Method in Java? Can it be Overridden?
A final method cannot be overridden in subclasses. Declaring a method as final prevents any further overriding in child classes.
For example:
public class Parent { public final void print() { System.out.println("Parent print"); }}public class Child extends Parent { public void print() { //Compile time error System.out.println("Child print"); }}
Trying to override a final method results in a compile time error. Final methods enforce non-polymorphic behavior.
What are Concrete and Abstract Classes in Java?
A concrete class provides complete implementation for all its methods. An abstract class contains both implemented and unimplemented methods.
- Concrete classes can be instantiated directly
- Abstract classes must be extended by subclasses to provide implementations for abstract methods before instantiation
For example:
public class ConcreteClass { public void concreteMethod() { //Some implementation }}public abstract class AbstractClass { public abstract void incompleteMethod(); public void concreteMethod() { //Some implementation }}
Abstract classes enable polymorphism through abstract method implementations provided by subclasses.
Explain the Interface in Java and Polymorphism
Interfaces in Java contain only abstract, non-implemented methods. All methods declared in an interface are implicitly public and abstract.
A class implements an interface and provides implementations for its abstract methods. This enables polymorphism when interface type references are used to refer to the implementing classes.
For example:
public interface Shape { void draw(); //Abstract method}public class Circle implements Shape { @Override public void draw() { //Implementation to draw a circle }}
Here, Circle
implements Shape
and provides its own draw()
implementation.
What is Object Type Casting in Java?
Type casting allows explicit conversion of an object from one data type to another. There are two types of casting:
- Upcasting – Casting a subclass object to a superclass object (widening conversion)
- Downcasting – Casting a superclass object to a subclass object (narrowing conversion)
For example:
Parent p = new Child(); // Upcasting Child c = (Child)p; // Downcasting back to Child type
Upcasting enables polymorphism by allowing subclass objects to be treated as superclass objects. Downcasting is required to access specialized subclass behaviors.
How to Achieve Runtime Polymorphism in Java?
There are two ways to achieve runtime polymorphism in Java:
-
Method overriding – Provide specialized method implementations in subclasses
-
Object type casting – Reference subclass objects using superclass variables
For example:
// Method overridingParent p = new Child(); p.print(); // Calls Child's print()// Object type castingParent p = new Child();Child c = (Child)p;c.print(); // Calls Child's print()
This allows polymorphic behavior based on the runtime object type, not the compile-time reference type.
Polymorphism is a critical concept for OO programming. Mastering polymorphism interview questions demonstrates strong analytical and problem-solving skills.
Key techniques like method overloading/overriding, abstract classes, interfaces, upcasting, downcasting all enable polymorphic behavior in Java.
Practice these interview questions until you can explain polymorphism concepts clearly and concisely. This will ensure you are well-prepared to answer polymorphism questions for Java developer interviews.
Polymorphism Interview Questions | Java Interview Questions
FAQ
How do you explain polymorphism in an interview?
How do you demonstrate polymorphism?
What are the two 2 types of polymorphism?
How do you explain polymorphism in real life?
How do I prepare for a polymorphism interview?
Prepare for the types of questions you are likely to be asked when interviewing for a position where Polymorphism will be used. Polymorphism is a programming concept that allows developers to write code that is more flexible and adaptable. This concept can be difficult to understand and properly explain during a job interview.
How do I learn Java polymorphism interview questions?
Study common interview questions: Look for common Java polymorphism interview questions online and practice answering them.
Why is polymorphism important in a job interview?
Polymorphism is a programming concept that allows developers to write code that is more flexible and adaptable. This concept can be difficult to understand and properly explain during a job interview. However, being able to effectively communicate your understanding of polymorphism can make you stand out from other candidates.
How is polymorphism achieved in Java?
In Java, polymorphism is primarily achieved through method overriding and interface implementation. Method overriding involves creating a subclass with a method that already exists in its superclass. This allows the subclass to provide its own implementation of the method, giving it a different behavior than the superclass.