What is polymorphism?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for OOP Concepts development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Polymorphism (Greek: "many forms") allows objects of different types to be treated through a common interface. The correct method is selected at runtime based on the actual object type. "One interface, multiple implementations." Types: (1) Runtime polymorphism (dynamic dispatch/method overriding): achieved through inheritance and virtual methods. The actual method called is determined at runtime: abstract class Animal { public abstract void speak(); } class Dog extends Animal { public void speak() { System.out.println("Woof!"); } } class Cat extends Animal { public void speak() { System.out.println("Meow!"); } } class Duck extends Animal { public void speak() { System.out.println("Quack!"); } } // Polymorphic code: List<Animal> animals = new ArrayList<>(); animals.add(new Dog()); animals.add(new Cat()); animals.add(new Duck()); for (Animal a : animals) { a.speak(); // Correct method called for each actual type! } // Woof! Meow! Quack!; (2) Compile-time polymorphism (method overloading / ad-hoc polymorphism): multiple methods with same name, different parameters: class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } } // Compiler selects correct version based on argument types.. Parametric polymorphism (generics/templates): List<Integer> ints; List<String> strings;. Polymorphism enables the Open/Closed Principle — add new types without changing existing code that processes them.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.
Previous
What is inheritance in OOP?
Next
What is the difference between overloading and overriding?