Define polymorphism in Java with examples
๐ก Concept: Polymorphism in Java
Polymorphism is an OOP principle that allows objects to take multiple forms, enabling flexibility and dynamic method binding.
๐ Quick Intro
Java supports two types of polymorphism: compile-time (method overloading) and runtime (method overriding).
๐ง Analogy
Think of a person who can act as a teacher, a parent, or a student depending on the situation โ polymorphism lets one entity behave differently.
๐ง Technical Explanation
- Compile-time polymorphism achieved via method overloading (same method name, different parameters).
- Runtime polymorphism achieved via method overriding (subclass changes superclass method behavior).
- Enables flexible and maintainable code by allowing methods to perform differently based on object type.
- Supports dynamic method dispatch in Java.
๐ฏ Use Cases
- โ Implement multiple behaviors using the same method name.
- โ Enable runtime flexibility via subclass-specific method implementations.
- โ Use polymorphism to design extensible systems.
๐ป Java Polymorphism Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Dog barks
a = new Cat();
a.sound(); // Cat meows
}
}

โ Interview Q&A
Q1: What is polymorphism?
A: Ability of an object to take many forms.
Q2: What is method overloading?
A: Same method name with different parameters.
Q3: What is method overriding?
A: Subclass changes superclass method behavior.
Q4: Which type of polymorphism is compile-time?
A: Method overloading.
Q5: Which type is runtime polymorphism?
A: Method overriding.
Q6: How does Java support polymorphism?
A: Through inheritance and interfaces.
Q7: What is dynamic method dispatch?
A: Runtime decision on which method to call.
Q8: Can polymorphism improve maintainability?
A: Yes.
Q9: Does Java support polymorphism with interfaces?
A: Yes.
Q10: Why is polymorphism important?
A: Enables flexible and extensible code.
๐ MCQs
Q1. What is polymorphism?
- Encapsulation
- Inheritance
- Ability of an object to take many forms
- Abstraction
Q2. What is method overloading?
- Different method names
- Same method name with different parameters
- Interface methods
- Abstract methods
Q3. What is method overriding?
- Overloading methods
- Subclass changes superclass method behavior
- Interface implementation
- Class inheritance
Q4. Which is compile-time polymorphism?
- Method overriding
- Method overloading
- Dynamic dispatch
- Encapsulation
Q5. Which is runtime polymorphism?
- Method overloading
- Method overriding
- Static methods
- Constructors
Q6. How does Java support polymorphism?
- Only inheritance
- Only interfaces
- Through inheritance and interfaces
- None
Q7. What is dynamic method dispatch?
- Compile-time method call
- Runtime decision on method call
- Method overloading
- Method hiding
Q8. Can polymorphism improve maintainability?
- No
- Yes
- Maybe
- Sometimes
Q9. Does Java support polymorphism with interfaces?
- No
- Yes
- Sometimes
- Rarely
Q10. Why is polymorphism important?
- Slows down code
- Enables flexible and extensible code
- Increases complexity
- Reduces readability
๐ก Bonus Insight
Polymorphism is a cornerstone of Javaโs OOP capabilities, enabling flexible, maintainable, and extensible software design.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!