Polymorphism in java in hindi के इस tutorial में हम सीखेंगे की what is java Polymorphism in hindi और साथ ही हम सीखेंगे benefits of java Polymorphism in hindi
Polymorphism in java in hindi
polymorphism एक ऐसी प्रक्रिया है जिसमें किसी object के behaviour को दूसरा object update या modified करके अपने कार्य में इस्तेमाल करता है इसे प्राय method overriding और method overloading द्वारा achieve किया जाता है।

polymorphism यह दो शब्द से मिलकर बना है Poly और morph जिसका मतलब है किसी वस्तु की कई अवस्थाएं होना।
उदाहरण के तौर पर जल के तीन अवस्थाएं होते है ठोस द्रव और गैस।
Real world example of polymorphism in java in hindi
धरती पर कई सारे animals पाए जाते हैं मगर हर animals के अपने-अपने behaviour हैं उदाहरण के तौर पर ज्यादातर animals आवाज निकाल सकते हैं मगर हर animal की आवाज एक दूसरे से अलग होते है।
इस image से आप बेहतर तरीके से समझेंगे।

इस image में हमने एक class बनाया जिसका नाम Animal है और उसके अंदर आपने एक method बनाए ( void sound() )
फिर हमने तीन class बनाया Lion, Dog और Cat फिर तीनों classes ने animal class को extends कर उसके method sound को override किया।
और तीनों ने अपने अपने अंदाज मे आवाज को निकाला।
Definition of polymorphism in java in hindi
polymorphism एक प्रक्रिया है जिसमें यह object की स्थिति को दूसरा object अपने मन मुताबिक modified कर अपने काम में लाता है।
यह प्रायः inheritance के समान होता है मगर यह inheritance से थोड़ा अलग होता है।
क्योंकि inheritance मे parent class child class के property को grab करता है।
Also read : inheritance in java in hindi
मगर polymorphism में child class parent class की property को इस्तेमाल कर उसमें modification कर अपने काम में लाता है।
Important points of polymorphism in java in hindi
- इसमें parent class की property को child class अपने मन मुताबिक modified करता है।
- polymorphism method overloading और method overriding द्वारा achive किया जाता है।
- polymorphism दो प्रकार के होते हैं runtime polymorphism और compile time polymorphism
Example programme of polymorphism in java in hindi
class Animal {
void sound () {
System.out.println("sound");
}
}
class Dog extends Animal {
void sound () {
System.out.println("wooo wooo");
}
}
class Cat extends Animal {
void sound () {
System.out.println("meow meow");
}
}
class Programme {
public static void main (String[] args) {
Dog d = new Dog();
d.sound();
Cat c = new Cat();
c.sound();
}
}
Output :
wooo wooo
meow meow
Compile-time Polymorphism in java in hindi
compile-time polymorphism को method overloading भी कहा जाता है।
क्योंकि जब method को call करते हैं तो java compiler method signature द्वारा यह पता करता है कि कौन से method को किस समय call करना है
Also read : Methods in java in hindi
उदाहरण के तौर पर आपने एक class में दो method बनाए जिसके नाम same है मगर उसके parameter different है। for example पहला मैं int parameter दूसरा String
तब main method में जिस parameter के साथ आप method को call करेंगे वह method execute होगा।
Example programme of compile-time Polymorphism
class Example {
void fun (int a) {
System.out.println(a);
}
void fun (String a) {
System.out.println(a);
}
}
class Programme {
public static void main (String[] args) {
Example obj = new Example();
obj.fun("amount ");
obj.fun(100);
}
}
Output :
amount 100
क्योंकि String parameter वाले method को हमने पहले call किया उसके बाद int parameter वाले method को
Runtime polymorphism in java in hindi
runtime polymorphism को method overriding भी कहा जाता है।
जिसमें child class parent class के method को override कर अपने मन मुताबिक बनाता है।
चुकी parent class और child class के method के signature same होते हैं इसलिए java compiler run time में यह decide करता है
कि parent class के method को call करना है या child class के method अगर child class ने parent class के method को override किया हो
और child class के object द्वारा उस method को call किया हो तब java compiler child class में overriden method को call करेगा
और अगर overriden नहीं किया हो तो वह parent class के method को call करेगा।
Example programme of runtime Polymorphism
class Animal {
void sound () {
System.out.println("sound");
}
}
class Dog extends Animal {
void sound () {
System.out.println("wooo wooo");
}
}
class Programme {
public static void main (String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output :
wooo wooo
Conclusion
आज आपने सीखा कि polymorphism क्या होता है। इसे कैसे इस्तेमाल करते है
इसे के साथ आज का polymorphism in java in hindi tutorial समाप्तः होता है आपको येह tutorial कैसा लगा जल्दी से comment करे 👇
Happy coding 😊😀