Skip to main content
Java Basics
CHAPTER 16 Beginner

Polymorphism in Java

Updated: May 17, 2026
5 min read

# CHAPTER 16

Polymorphism in Java

1. Introduction

Polymorphism means "many forms." In Java, a single method or reference can behave differently based on context. A parent reference can hold a child object, and the correct method is called at runtime. This is the cornerstone of flexible, extensible software design.

2. Types of Polymorphism

Compile-time (Static): Method Overloading — resolved at compile time. Runtime (Dynamic): Method Overriding — resolved at runtime via dynamic dispatch.

3. Compile-Time Polymorphism (Overloading)

java
12345
class Calculator {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
    int add(int a, int b, int c) { return a + b + c; }
}

4. Runtime Polymorphism (Overriding)

java
1234567891011121314151617
class Shape {
    void draw() { System.out.println("Drawing a shape"); }
}
class Circle extends Shape {
    @Override
    void draw() { System.out.println("Drawing a circle"); }
}
class Square extends Shape {
    @Override
    void draw() { System.out.println("Drawing a square"); }
}

// Dynamic dispatch
Shape s1 = new Circle();
Shape s2 = new Square();
s1.draw(); // "Drawing a circle" — calls Circle's version at RUNTIME
s2.draw(); // "Drawing a square"

5. The instanceof Operator

java
1234
Shape s = new Circle();
System.out.println(s instanceof Circle); // true
System.out.println(s instanceof Shape);  // true
System.out.println(s instanceof Square); // false

6. Upcasting and Downcasting

java
12345
// Upcasting (automatic): Child → Parent
Shape shape = new Circle();

// Downcasting (manual): Parent → Child
Circle circle = (Circle) shape;  // Must be sure it's actually a Circle!

7. MCQ Quiz with Answers

Question 1

Polymorphism means:

Question 2

Method overloading is:

Question 3

Method overriding is:

Question 4

Dynamic method dispatch determines:

Question 5

Shape s = new Circle(); s.draw(); calls:

Question 6

instanceof checks:

Question 7

Upcasting is:

Question 8

Can you override a static method?

Question 9

Can you override a private method?

Question 10

Which enables runtime polymorphism?

8. Interview Questions

  • Q: Explain compile-time vs runtime polymorphism.
  • Q: What is dynamic method dispatch?
  • Q: Can constructors be polymorphic?
  • Q: What is the difference between method hiding and method overriding?

9. Summary

Polymorphism allows one reference to behave differently based on the actual object type. Compile-time polymorphism uses overloading; runtime polymorphism uses overriding with inheritance. Dynamic dispatch ensures the correct method is called at runtime.

10. Next Chapter Recommendation

In Chapter 17: Encapsulation and Abstraction, we'll learn how to protect data and hide complexity.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·