आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
Java - Polymorphism

Polymorphism ये Object Oriented Programming का बहुत ही अच्छा feature है |

एक ही रूप में अनेक रूप होना polymorphism होता है |

Polymorphism ये शब्द 'poly' और 'morph' इन दो शब्दों को मिलकर बनाया गया है |

Real Life Example for Polymorphism

Real life में smartphones की कई कंपनियां है | जब smartphone इस्तेमाल किया जाता है, तब उस mobile में कई सारे features होते है जैसे Camera, Calling, Music Player, Video Player.
यहाँ पर mobile ये नाम तो एक ही है लेकिन इसमें कई सारे forms है |

Example for Run-Time Polymorphism

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(Upcasting)

दिए हुए 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 के लिए दो प्रकार है |

  1. Compile-Time Polymorphism
  2. Run-Time Polymorphism

1. Compile-Time Polymorphism : Compile Time Polymorphism को Static Polymorphism भी कहा जाता है | यहाँ पर Method Overloading होता है | जिसमे एक ही प्रकार के methods के नाम और उनके parameters और उन parameters के types अलग-अलग होता है |

Method Overloading

Source Code :
class Sample{
		
	public void disp(int x, int y){
		System.out.println("Value of x : " + x);
		System.out.println("Value of y : " + y);
	}
	public void disp(int x){
		System.out.println("Value of x : " + x);
	}	
	public static void main(String[] args){
	
	Sample s = new Sample();
	s.disp(5, 6);
	s.disp(7);
	}
}
Output :
Value of x : 5
Value of y : 6
Value of x : 7

2. Run-Time Polymorphism : Run-Time Polymorphism में JVM द्वारा method को run time पर call किया जाता है | Method Overriding ये Run-Time Polymorphism का एक ख़ास उदाहरण है |

Source Code :
class A
{
    void disp(){
		
        System.out.println ("class A");
    }
}

class B extends A
{
    void disp(){
		
        System.out.println ("Class B");
    }
}
class C{
	
	public static void main (String args []) {
	A obj1 = new A();
	obj1.disp();
	   
	A obj2 = new B();
	obj2.disp();
	}
}
Output :
Class A
Class B