polymorphism theory in java
- Get link
- X
- Other Apps
What is polymorphism ?
Polymorphism हम उसको बोलते है जिसके multipal form होते है
Like as:- water के different form होते है जेसे liquid,solid,gas,
Shapes
Polymorphism मतलब होता है वो चीज एक ही है but form अलगअलग होता है
Types ऑफ़ polymorphism
1.compile time polymorphisms/static polymorphism
Compile time polymorphism हम
Method overloading के through achive करसकते है
इसे compiler handle करता है
2.run time polymorphism /Dynamic polymorphism
Run time polymorphism हमलोग achieve करते है by method overriding.
इसे jvm handle करता है .
Method overloading | Method overriding |
मतलबदो या दो से जादा method का name same होना चाहिये
दोनों method same class में होना चाहिए
:- no ऑफ़ arg :- seq ऑफ़ arg :- type ऑफ़ arg Class test { Void show() { s.o.p(“1”); } Void show() { S.o.P(“2”); } Public static void main(String arg[]){ Test s1= new Test(); s1.show (); } Ye program error show karega kuki show name ka do method hai compile ko pata nahi kon sa show method ko call karu No of argClass test { Void show(int a) { s.o.p(“1”); } Void show(int a,int b) { S.o.P(“2”); } Public static void main(String arg[]){ Test s1= new Test(); s1.show (5); s1.show (5,4); } :- seq ऑफ़ argClass test { Void show(int a,string b) { s.o.p(“1”); } Void show(String a,int b) { S.o.P(“2”); } Public static void main(String arg[]){ Test s1= new Test(); s1.show (a ,” sonu”); s1.show (sonu,” a”); } :- type ऑफ़ argClass test { Void show() { s.o.p(“1”); } Void show() { S.o.P(“2”); } Public static void main(String arg[]){ Test s1= new Test(); s1.show (); } :- type ऑफ़ argClass test { Void show(int a) { s.o.p(“1”); } Void show(String b) { S.o.P(“2”); } Public static void main(String arg[]){ Test s1= new Test(); s1.show (); } 1.can we achieve method overloading by changing the return type of method only? Ans:- in java, method overloading is not possible by chabging the return type of the method only because of ambiguity. Class test { Void show(int a) { s.o.p(“1”); } String show(int a) { S.o.P(“2”); } Public static void main(String arg[]){ Test s1= new Test(); s1.show (); } Type_of_arg_4.java Type_of_arg_4.java:17: error: method show(int) is already defined in class Type_of_arg_4 void show (int a){ ^ 1 error 2. Can we overload java main() method? Ans:- yes ,we can have any number of main method overloading.This is because JVM always calls main() method which receives string arrays as arguments only. Class Test { Public static void main(String[] argos) { System.out.println(“ 1”); } Public static void main(int a); { System.out.println(“ 2”); } } Case 1Class Test { Public static void main(String[] argos) { System.out.println(“ 1”); Test t= new test(); t.main(20); } Public static void main(int a); { System.out.println(“ 2”); } } Class Test { Void show(int a) { System.out.println(“int methos “); } Void show(String a) { System.out .prontln(“ string method”); } Public stativc void main(String[] args) { Test t= new Test(); t.show(30); t.show(“ sdf”); t.show(‘ a’); automatic promotion } } automatic promotion One type is promoted to anothe implicitly if no matching datatype id found. Below is the diagram: Calse 2Class Test { Void show (obj a) { system.out.println(“ object method”); } Void show (String a) { System.out.prinln(“ Stirng methd); } Public static void main(String[] argos) { Test t= new Test(); t.show(‘ s’ ) ; t.show(“ sba”); } } Case 3Class Test { Void show (StrjgBuffer a) { system.out.println(“ Stringbuffer object method”); } Void show (String a) { System.out.prinln(“ Stirng methd); } Public static void main(String[] argos) { Test t= new Test(); t.show(“ sba”); t.show(new StringBuffer(“ xyz”)); t.show(null); } } Case 4Class Test { Void show(int a,float b) { System.out.priltn(“ int flaot method”); } Void show(float a, int b) { System.out.prinln(“ float int method”); } Publci static vodi main(Stirng[] args) { TEst t= new Test(); t.show(10,50.01f); t.show(50.01f,40); t.show(10,20); Errow reference to show is ambiguous That mean ye automatic permote nahi ho paraha hai } } Class Test { Void show(Stirng a,float b) { System.out.priltn(“ int flaot method”); } Void show(float a, int b) { System.out.prinln(“ float int method”); } Publci static vodi main(Stirng[] args) { TEst t= new Test(); //t.show(10,50.01f); t.show(50.01f,40); t.show(“sbc”,20); } ] Case 5Class Test{ Void show(int a) { System.out.prinln(“ int method”); } Void show(int.. a) { System.out .println(varags method); { Public static vodi main(){ Test t= new Test(); t.show(45); t.show(45,45,24); t.show();’ }
|
:- no ऑफ़ arg :- seq ऑफ़ arg :- type ऑफ़ arg
Class test { Void show() { System.out.println(“1”); } } Class xyz { Vodi show() { System.out.println(“2”); } Public static void main(String args[]) { Test s1= new Test(); Xyz s2= new xyz(); s1.show(); s2.show(); } :- no ऑफ़ argClass test { Void show() { System.out.println(“1”); } } Class xyz { Vodi show() { System.out.println(“2”); } Public static void main(String args[]) { Test s1= new Test(); Xyz s2= new xyz(); s1.show(); s2.show(); } :- type ऑफ़ argClass test { Void show(String a) { System.out.println(“1”); } } Class xyz { Vodi show(String a) { System.out.println(“2”); } Public static void main(String args[]) { Test s1= new Test(); Xyz s2= new xyz(); s1.show(); s2.show(); } :- seq ऑफ़ argClass test { Void show(String a, int b ) { System.out.println(“1”); } } Class xyz { Vodi show(String a, int b) { System.out.println(“2”); } Public static void main(String args[]) { Test s1= new Test(); Xyz s2= new xyz(); s1.show(); s2.show(); } Case 1 1.Do overring method must have same return type (or subtype)?Class Test { Void show() { System.out.println(“1”);} } Class xyz extends test Void show() { system.out.pritln(“2”); } Publc static void main(String args[]) { } } |
- Get link
- X
- Other Apps
Comments
Post a Comment