Java Polymorphism In Hindi - Java Polymorphism क्या है ?
Polymorphism ये Object Oriented Programming का बहुत ही अच्छा feature है |
एक ही रूप में अनेक रूप होना polymorphism होता है |
Polymorphism ये शब्द 'poly' और 'morph' इन दो शब्दों को मिलकर बनाया गया है |
Real Life Example for Polymorphism
यहाँ पर 'mobile' name तो एक ही है लेकिन इसमें कई सारे forms हैं |
Real life में smartphones की कई कंपनियां हैं | जब smartphone इस्तेमाल किया जाता है, तब उस mobile में कई सारे features होते हैं जैसे Camera, Calling, Music Player, Video Player आदि |
Source Code :
class Smartphones{
int camera(){
return 0;
}
}
class Microsoft extends Smartphones{
int camera(){
return 8;
}
}
class Samsung extends Smartphones{
int camera(){
return 12;
}
}
class Apple extends Smartphones{
int camera(){
return 12;
}
}
class Sample{
public static void main(String args[]){
Smartphones s = new Microsoft();
System.out.println("Microsoft Camera : " + s.camera() + "MP");
s = new Samsung();
System.out.println("Samsung Camera : " + s.camera() + "MP");
s = new Apple();
System.out.println("Apple Camera : " + s.camera() + "MP");
}
}
Output :
Microsoft Camera : 8MP
Samsung Camera : 12MP
Apple Camera : 12MP
Example for Run - Time Polymorphism :
दिए हुए program में A और B ये दो classes लिए है | B class; A class को inherit कर रहा है और दोनों ही class में disp() नाम का same method है | आखिर में parent class के reference variable से child class के object को refer किया गया है |super class के reference variable से disp() ये method call किया गया है | यहाँ पर compile-time पर उन दो classes के same methods में से कौनसा method call करना ये समझ नहीं आता | इसीलिए JVM द्वारा इसे Run-time पर उस method को call किया जाता है |
Source Code :
//B.java
class A{
void disp(){
System.out.println("class A");
}
}
class B extends A{
void disp(){
System.out.println("class B");
}
public static void main(String args[]){
A a = new B(); //upcasting
a.disp();
}
}
Output :
class B
Java में Polymorphism के लिए दो प्रकार है |
- Compile-Time Polymorphism
- Run-Time Polymorphism
1. Compile-Time Polymorphism :
Compile Time Polymorphism को Static Polymorphism भी कहा जाता है | यहाँ पर Method Overloading होता है | जिसमे एक ही प्रकार के methods के नाम और उनके parameters और उन parameters के types अलग-अलग होता है |
2. Run-Time Polymorphism :
Run-Time Polymorphism में JVM द्वारा method को run time पर call किया जाता है |
अगर आपको यह पोस्ट 📑 पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !