← Back to list

Polymorphism in Java — OOP Concept Explained with Real-World Examples | Interview Question

Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP) in Java. The word Polymorphism comes from two Greek…

Tejshree Bakore · 2026-05-23 22:41 · 0 claps · 3.7 min read
#polymorphism #oop-concepts #oops-interview-questions #java #interview-questions
Open on Medium ↗
Wiki topics: 💻 · Programming

Polymorphism in Java — OOP Concept Explained with Real-World Examples | Interview Question

Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP) in Java. The word Polymorphism comes from two Greek words:

  • Poly → Many
  • Morphism → Forms

So, polymorphism means:

“One thing behaving in multiple forms.”

In Java, polymorphism allows the same method or object to perform different actions depending on the situation.

The Philosophical Perspective

Essence vs. Existence: Polymorphism embodies existential philosophy by separating what an object is from how it acts. The abstract concept of an object (the blueprint or contract) dictates its meaning, but its concrete execution is defined by its current context and experiences.

Adaptability: It reflects the philosophical concept of adaptation. An entity does not have to be rigid to maintain identity; instead, it fluidly transforms its behavior based on the environment while still being recognizable.

Why is Polymorphism Important?

Polymorphism helps developers:

  • Write reusable code
  • Improve flexibility
  • Reduce code duplication
  • Achieve method overriding and method overloading
  • Build scalable applications

It is heavily used in:

  • Frameworks
  • APIs
  • Spring Boot
  • Selenium Automation
  • Collections Framework

Real-World Example of Polymorphism

Imagine a person behaves differently in different situations:

  • At office → Employee
  • At home → Parent
  • With friends → Friend

Same person, different behavior.

Same person, different behavior.

Similarly in Java:

  • Same method name
  • Different behavior

That is polymorphism.

Types of Polymorphism in Java

Java supports two types of polymorphism:

1. Compile-Time Polymorphism

Also called:

  • Method Overloading
  • Static Polymorphism

2. Runtime Polymorphism

Also called:

  • Method Overriding
  • Dynamic Polymorphism

1. Compile-Time Polymorphism (Method Overloading)

Method overloading happens when:

  • Multiple methods have the same name
  • But different parameters

The compiler decides which method to call during compile time.

Example: Calculator Program

class Calculator {

 // Method with 2 integer parameters
 void add(int a, int b) {
 System.out.println("Sum: " + (a + b));
 }

 // Method with 3 integer parameters
 void add(int a, int b, int c) {
 System.out.println("Sum: " + (a + b + c));
 }

 // Method with double parameters
 void add(double a, double b) {
 System.out.println("Sum: " + (a + b));
 }
 }

 public class Main {
 public static void main(String[] args) {

 Calculator c = new Calculator();

 c.add(10, 20);
 c.add(10, 20, 30);
 c.add(5.5, 2.5);
 }
 }
Output
Sum: 30
 Sum: 60
 Sum: 8.0

Key Points of Method Overloading

✔ Same method name ✔ Different parameters ✔ Happens within same class ✔ Decided at compile time

2. Runtime Polymorphism (Method Overriding)

Method overriding occurs when:

  • Parent class method is redefined in child class
  • Method signature remains same
  • JVM decides method at runtime

Real-World Example

A vehicle can have different types:

  • Car
  • Bike
  • Bus

All vehicles can start(), but each starts differently.

Example: Vehicle Program
class Vehicle {

 void start() {
 System.out.println("Vehicle starts");
 }
 }

 class Car extends Vehicle {

 @Override
 void start() {
 System.out.println("Car starts with key");
 }
 }

 class Bike extends Vehicle {

 @Override
 void start() {
 System.out.println("Bike starts with self-start button");
 }
 }

 public class Main {

 public static void main(String[] args) {

 Vehicle v;

 v = new Car();
 v.start();

 v = new Bike();
 v.start();
 }
 }
Output
Car starts with key
 Bike starts with self-start button
How Runtime Polymorphism Works
Vehicle v = new Car();

Here:

  • Reference variable → Parent class (Vehicle)
  • Object created → Child class (Car)

At runtime, JVM checks the actual object type and calls the overridden method.

This is called: Dynamic Method Dispatch

Important Rules of Method Overriding

✔ Rules

  • Method name must be same
  • Parameters must be same
  • IS-A relationship required (Inheritance)
  • Cannot reduce access modifier
  • Static methods cannot be overridden

Key Differences:

Advantages of Polymorphism

1. Code Reusability- Same method can be reused for different behaviors.

2. Flexibility- Easily extend functionality.

3. Loose Coupling- Code becomes maintainable and scalable.

4. Better Readability- Cleaner and simpler code structure.

Real-Time Example in Selenium Automation

Polymorphism is widely used in Selenium frameworks.

*WebDriver driver;

driver = new ChromeDriver();

driver = new FirefoxDriver();*

Here:

  • WebDriver is interface/reference
  • ChromeDriver and FirefoxDriver are implementations

Same reference → Different behaviors.

Interview Questions on Polymorphism

Q1. What is polymorphism in Java?

Polymorphism allows one method, object, or interface to behave differently in different situations.

Q2. What are the types of polymorphism?

  1. Compile-Time Polymorphism
  2. Runtime Polymorphism

Q3. Can static methods be overridden?

No. Static methods belong to class, not objects.

Q4. Why is runtime polymorphism called dynamic?

Because JVM decides which method to execute during runtime.

Quick Revision

Compile-Time Polymorphism

→ Method Overloading

Runtime Polymorphism

→ Method Overriding

Main Benefit

→ Same method behaves differently based on object type

Conclusion

Polymorphism is one of the most powerful concepts in Java OOP.

It allows:

  • One interface
  • Multiple implementations

Using polymorphism makes applications:

✔ Flexible ✔ Reusable ✔ Maintainable ✔ Scalable

Understanding polymorphism is essential for mastering:

  • Core Java
  • Frameworks
  • Automation Testing
  • Enterprise Applications

메타데이터
post_id
8f4ab60ef9ef
slug
polymorphism-in-java-oop-concept-explained-with-real-world-examples-8f4ab60ef9ef
url
https://medium.com/@knowledge.enlightens22/polymorphism-in-java-oop-concept-explained-with-real-world-examples-8f4ab60ef9ef
canonical_url
https://medium.com/@knowledge.enlightens22/polymorphism-in-java-oop-concept-explained-with-real-world-examples-8f4ab60ef9ef
author_url
https://medium.com/@knowledge.enlightens22
status
ok
fetched_at
2026-06-09 15:37:30