Java Topics
Polymorphism
Last Updated : 26 May, 2026
title: Polymorphism in Java
title: Polymorphism in Java description: Compile-time aur runtime polymorphism
Poly = Many, Morph = Forms. Same method alag-alag forms mein kaam karna.
1. Compile-time Polymorphism (Method Overloading)
Same method name, different parameters. Compile time par decide hota hai.
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; }
String add(String a, String b) { return a + b; }
}
Calculator calc = new Calculator();
calc.add(5, 3); // 8
calc.add(3.14, 2.5); // 5.64
calc.add(1, 2, 3); // 6
calc.add("Hello", " World"); // "Hello World"2. Runtime Polymorphism (Method Overriding)
Parent class reference child class object hold karta hai. Runtime par decide hota hai.
class Shape {
void draw() { System.out.println("Shape draw ho rahi hai"); }
}
class Circle extends Shape {
@Override
void draw() { System.out.println("Circle draw ho rahi hai"); }
}
class Rectangle extends Shape {
@Override
void draw() { System.out.println("Rectangle draw ho raha hai"); }
}
// Runtime Polymorphism
Shape s;
s = new Circle(); s.draw(); // "Circle draw ho rahi hai"
s = new Rectangle(); s.draw(); // "Rectangle draw ho raha hai"Dynamic Method Dispatch
Shape[] shapes = {new Circle(), new Rectangle(), new Circle()};
for (Shape shape : shapes) {
shape.draw(); // Runtime par actual type decide hota hai
}Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Polymorphism.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, oops
Related Java Topics