Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 20

Polymorphism

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

What is Polymorphism?

Polymorphism means "many forms". In Java it lets one method name behave differently depending on the object or the arguments. It is one of the four pillars of OOP and makes code flexible and extensible.

💡 Meaning

Poly = many, morph = forms. The same action (e.g. draw()) takes many forms for different shapes.

Two Types of Polymorphism

TypeAlso calledHow
Compile-timeMethod OverloadingSame method name, different parameters — resolved by the compiler.
RuntimeMethod OverridingChild class redefines a parent method — resolved at runtime.

1) Overloading (compile-time)

Java
class Calc {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }  // same name, different types
}
▶ Output
add(2,3) → 5    add(2.5,1.5) → 4.0

2) Overriding (runtime)

Java
class Animal { void sound() { System.out.println("Some sound"); } }
class Cat extends Animal {
    void sound() { System.out.println("Meow"); }  // overrides parent
}
Animal a = new Cat();
a.sound();  // runtime picks Cat's version
▶ Output
Meow

Why It's Useful

  • Write general code (e.g. Animal a) that works for many specific types.
  • Add new subclasses without changing existing code.
  • Cleaner APIs — one method name instead of many.
⚠️ Overloading vs Overriding

Overloading = same class, different parameters (compile-time). Overriding = parent & child, same signature (runtime). Don't confuse the two.

Summary

  • Polymorphism = one name, many forms.
  • Compile-time (overloading) and runtime (overriding).
  • Makes code flexible, reusable and easy to extend.

Polymorphism क्या है?

Polymorphism का मतलब है "कई रूप"। Java में यह एक ही method नाम को object या arguments के हिसाब से अलग व्यवहार करने देता है। यह OOP के चार स्तंभों में से एक है और code को flexible बनाता है।

💡 मतलब

Poly = कई, morph = रूप। एक ही action (जैसे draw()) अलग-अलग shapes के लिए कई रूप लेता है।

Polymorphism के दो प्रकार

प्रकारदूसरा नामकैसे
Compile-timeMethod Overloadingएक ही नाम, अलग parameters — compiler तय करता है।
RuntimeMethod OverridingChild class parent method दोबारा define करे — runtime पर तय।

1) Overloading

Java
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
▶ Output
add(2,3) → 5    add(2.5,1.5) → 4.0

2) Overriding

Java
class Cat extends Animal {
    void sound() { System.out.println("Meow"); }
}
Animal a = new Cat();  a.sound();
▶ Output
Meow

क्यों उपयोगी है

  • General code लिखें जो कई specific types के लिए काम करे।
  • नई subclasses जोड़ें बिना पुराना code बदले।
  • साफ़ APIs — एक नाम, कई काम।
⚠️ Overloading vs Overriding

Overloading = एक ही class, अलग parameters (compile-time)। Overriding = parent और child, एक ही signature (runtime)।

सारांश

  • Polymorphism = एक नाम, कई रूप।
  • Compile-time (overloading) और runtime (overriding)।
  • Code को flexible, reusable और extend करने में आसान बनाता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.