Java में Polymorphism
In this page:
ParentType variable = new ChildType();
variable.methodName(); // child's version runs
What is Polymorphism?
Polymorphism ('कई forms') अलग classes के objects को एक shared, common interface या parent type के through treat होने देता है, इसलिए calling code की वही line सही से अलग behavior trigger कर सकती है इस आधार पर कि यह किस असली object पर operate कर रही है।
उदाहरण: What is Polymorphism?
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // same call, different behavior depending on the object
}
}
Login to try C/C++/Java/PHP code in the editor
Compile-Time Polymorphism
Method overloading के ज़रिए हासिल किया गया Compile-time polymorphism, एक class को कई methods define करने देता है जो एक नाम share करते हैं लेकिन उनकी parameter lists में अलग होते हैं — compiler compile time पर arguments के types के आधार पर call करने के लिए सही version चुनता है।
उदाहरण: Compile-Time Polymorphism
// Define the class Main
public class Main {
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(add(2, 3));
// Print a line to the console
System.out.println(add(2.5, 3.5));
}
}
Login to try C/C++/Java/PHP code in the editor
Run-Time Polymorphism
Method overriding के ज़रिए Run-time polymorphism हासिल होता है, और कौन सा overridden method असल में चलता है इसका decision JVM द्वारा runtime पर object के असली, concrete class के आधार पर लिया जाता है — compiler द्वारा reference variable के declared type के आधार पर नहीं।
उदाहरण: Run-Time Polymorphism
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // decided at runtime by the object's real class
}
}
Login to try C/C++/Java/PHP code in the editor
Polymorphism with Data Members
Java में सिर्फ methods dynamic runtime dispatch में participate करते हैं; fields नहीं — अगर एक subclass उसी नाम वाली एक field से parent की field hide करता है, आपको कौन सी field वापस मिलती है यह compile time पर reference type पर निर्भर करता है, object की असली runtime class पर नहीं।
उदाहरण: Polymorphism with Data Members
class Animal {
String type = "Animal";
}
class Dog extends Animal {
String type = "Dog";
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
System.out.println(a.type); // "Animal": fields don't use dynamic dispatch
}
}
Login to try C/C++/Java/PHP code in the editor
Benefits of Polymorphism
Polymorphism loose coupling को promote करता है आपको ऐसे functions और methods लिखने देकर जो एक general parent type accept करते हैं और pass किए किसी भी subclass के साथ सही से काम करते हैं, पहले से हर possible subclass जानने की ज़रूरत के बिना जो आखिरकार मौजूद हो सकता है।
उदाहरण: Benefits of Polymorphism
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
}
class Cat extends Animal {
@Override
void sound() { System.out.println("Meow"); }
}
public class Main {
static void makeSound(Animal a) { // works with any subclass
a.sound();
}
public static void main(String[] args) {
makeSound(new Dog());
makeSound(new Cat());
}
}
Login to try C/C++/Java/PHP code in the editor
- एक parent reference के through सिर्फ child में मौजूद एक method call करना, जो एक compile error है।
- Fields के polymorphic होने की उम्मीद करना, जब वे reference type से चुने जाते हैं।
- Overloaded methods के run time पर चुने जाने की उम्मीद करना, जब overloads compile time पर चुने जाते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: