00:00
/* F01 - Polymorphism Definaion = Many + Forms 01- Method with different Forms 02- type = compile time ( static polymorphism), by m/t overloading ( complier handle) = run time ( dynamic polymorphism) by m/t overRiding ( JVM handle ) Method Overloading 1. multiple m/t with same name 2. must have same class 3. different arguments = number of arguments differenet . = sequence of arguments differenet . = type of arguments same . */
/*F01, m/t same argu.. */ class F_002_Test{ void show(){ System.out.println("1"); } void show(){ System.out.println("2"); } public static void main(String[] args) { F_002_Test t = new F_002_Test(); t.show(); // complier will get confused , error } } // F_002_Test.java:6: error: method show() is already defined in class F_002_Test
/*F02, diff. argu. */ class F_003_Test { void show(){ System.out.println("1"); } void show(int a ){ System.out.println("2"); } public static void main(String[] args) { F_003_Test t = new F_003_Test(); t.show(); t.show(10); } } // 1 // 2
/*F03, Diff. number of arg.. */ class F_004_Test { void show(int a, int b){ System.out.println("1"); } void show(int a ){ System.out.println("2"); } public static void main(String[] args) { F_004_Test t = new F_004_Test(); t.show(10); t.show(10,20); } } // 2 // 1
/*F04, unmatch data type in arg. */ class F_005_Test { void show(int a, String b){ System.out.println("1"); } void show(String a, int b ){ System.out.println("2"); } public static void main(String[] args) { F_005_Test t = new F_005_Test(); t.show(10,20); // error t.show(10,"Rajat"); t.show("Raj",20); } } // F_005_Test.java:11: error: no suitable method found for show(int,int) // t.show(10,20); // error // method F_005_Test.show(int,String) is not applicable // (argument mismatch; int cannot be converted to String) // method F_005_Test.show(String,int) is not applicable // (argument mismatch; int cannot be converted to String)
/*F05, diff. type of parameters */ class F_006_Test { void show(int a){//1 System.out.println("1"); } void show(String a){//2 System.out.println("2"); } public static void main(String[] args) { F_006_Test t = new F_006_Test(); t.show("abc"); // 2 t.show(10); // 1 } } // above called compiled time polymorphism // 2 // 1
/*F06, different return type + same mt */ class F_007_Test { void show(int a){ System.out.println("1"); } String show(int a){ System.out.println("2"); return a; // Added return statement for demonstration } public static void main(String[] args) { F_007_Test t = new F_007_Test(); t.show(10); // error } } // Method Defined in class Test already defined in class Test // not possible by changing the return type of the mehtod only because of ambiguity // The issue arises in the main method when you call t.show(10);. // The compiler cannot determine which show method to invoke because both methods // have the same name and the parameter types are not unique in this // context (you have int and String versions of show). The return type // alone does not help the compiler resolve the ambiguity. // 1. In Java, Method Overloading Is Determined Solely By The Method's Name And Parameter Types, Not The Return Type. // 2.So Even Though The Two Show() Methods Have Different Return Types (Void And String), // 3. They Are Still Considered As Candidates For Overloading Resolution, Leading To Ambiguity.
/*F07, Main m/t ol - diff. parameters */ class F_008_Test { public static void main(String[] args) { System.out.println("1"); } public static void main(int[] args) { System.out.println("2"); } } // compile , run successfully // 1 // The code compiles successfully because Java allows method overloading based on the // parameter types, and in this case, the two main methods have different parameter // types (String[] and int[]). // Execution: // Chat GPT- // 1. The Main Method With The String[] Parameter Is The One Typically Used As The Entry Point For A Java Program. // 2. When You Run The Program, It Will Print "1" To The Console Because The First Main Method Is Invoked By Default.
/*F08, ol main m/t by one argu.. */ class F_009_Test { public static void main(String[] args) { System.out.println("1"); F_009_Test t = new F_009_Test(); t.main(20); } public static void main(int[] args) { System.out.println("2"); } } // compile : no suitable method found for main(int) t.main(20); // method F_009_Test.main(String[]) is not applicable (argument mismatch; // int cannot be converted to String[]) method F_009_Test.main(int[]) is // not applicable (argument mismatch; int cannot be converted to int[]) // this is becaues JVM always calls main () m/t which receices strings
/*F09, Char to String, AutoPro. BSC DLF */ class F_010_Test { void show(int a){ System.out.println("int method"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { F_010_Test t = new F_010_Test(); t.show(10); t.show("Raj"); t.show('a'); } } // int method; // String method; // int method // pic 21 // byte promote ho jayega = short me . // Automatic Promotion = // 1. One type is promoted to another implicilty if no matching datatype is found. // 2. jo bhee arguments mai pass karta hu agar wo usko nahe milta hai to wo us // argument ko wo promote kar deyga // Chat GPT - // 1. T.Show('A');: Calls The Show Method With A Char Argument. // 2. In This Case, There Is No Method With An Exact Match For A Char, // 3. But Java Performs Automatic Promotion From Char To Int. // 4. So, The Show Method With The Int Parameter Will Be Called, And It Will Print "Int Method" To The Console.
/*F10, Char to (STRING + OBJ.) argu, AP */ class F_011_A_Temp { void show(Object a){ System.out.println("object method"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { F_011_A_Temp t = new F_011_A_Temp(); t.show('a'); } } // object method // object is the parent class of all the class in java // prefrence will give to child class // agar string se kaam chal saaktea hai to string ko he call karege // Chat GPT // t.show("Raj");: Calls the show method with a String argument. // The Java compiler determines which show method to call based on the actual argument's type. // In this case, it will call the show(String a) method, and "String method" will be printed to the console.
/*F11, String to (STRING + OBJ.) argu, AP */ class F_011_Temp { void show(Object a){ System.out.println("object method"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { F_011_Temp t = new F_011_Temp(); t.show("Raj"); } } // mac termial = String method // pic code10 // prefrence will give to child class // agar string se kaam chal saaktea hai to string ko he call karege // Chat GPT // t.show("Raj");: Calls the show method with a String argument. // The Java compiler determines which show method to call based on the actual argument's type. // In this case, it will call the show(String a) method, and "String method" will be printed to the console.
/*F12, String to (STRING + STRINGBuffer) argu, AP */ class F_012_Temp { void show(StringBuffer a){ System.out.println("String Buffer"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { F_012_Temp t = new F_012_Temp(); t.show("Raj"); } } // String method
/*F13, Stringbuffer to (STRING + STRINGBuffer) argu, AP */ class F_013_Test { void show(StringBuffer a){ System.out.println("String Buffer"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { F_013_Test t = new F_013_Test(); t.show(new StringBuffer ("xyz")); } } // String Buffer
/*F14, null to (STRING + STRINGBuffer) argu, AP */ class F_014_Test { void show(StringBuffer a){ System.out.println("String Buffer"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { F_014_Test t = new F_014_Test(); t.show(null); // error } } // compiler will get confused, shows ambigious error, wo confuse hai ki ye null value // me string ya string bufferr kis ke leeaye // Complie Time Error - // F_014_Test.java:11: error: reference to show is ambiguous // both method show(StringBuffer) in F_014_Test and method show(String) in F_014_Test match
/*F15, (int,float) and (float,int) to their respective m/t, AP */ class F_015_Test { void show(int a, float b){//1 System.out.println("int float method"); } void show(float a, int b){//2 System.out.println("Float int method"); } public static void main(String[] args) { F_015_Test t = new F_015_Test(); t.show(10,20.5f);//1 t.show(20.5f,10);//2 } } // pic 8 // int float m/t // Float int method
/*F16, (int, int) = (int, float) + (float,int),AP */ class F_016_Test { void show(int a, float b){ System.out.println("int float method"); } void show(float a, int b){ System.out.println("Float int m/t "); } public static void main(String[] args) { F_016_Test t = new F_016_Test(); t.show(10,20); // error ( can't promote) } } // agar single parameter hai to promote ho jayega , // multiple parameters promote nahe hoge // F_016_Test.java:11: error: reference to show is ambiguous // t.show(10,20); // error ( can't promote) // both method show(int,float) in F_016_Test and method show(float,int) in F_016_Test match
/*F17, (string,int) = (string ,float) + (float , int),AP */ class F_017_Test { void show(String a, float b){ System.out.println("int float method"); } void show(float a, int b){ System.out.println("Float int m/t "); } public static void main(String[] args) { F_017_Test t = new F_017_Test(); t.show("Raj", 20); } } // pic 8 // int float m/t // agar alag alag data type hai to wo usko promote kar deyga ,
/*F18, (float, float ) = (int,float) + (float,int), AP */ class F_018_Test { void show(int a, float b){ System.out.println("int float method"); } void show(float a, int b){ System.out.println("Float int m/t "); } public static void main(String[] args) { F_018_Test t = new F_018_Test(); t.show(10.5f,20.5f); // error } } // error // F_018_Test.java:11: error: no suitable method found for show(float,float) // t.show(10.5f,20.5f); // error // method F_018_Test.show(int,float) is not applicable // (argument mismatch; possible lossy conversion from float to int) // method F_018_Test.show(float,int) is not applicable // (argument mismatch; possible lossy conversion from float to int) // kyuki ye ek normal argument ki tarah kaam kareyga, // maan leyte hai ki ye promote kareyga usko to phir normal program jaha par // ek method float aur dusra method int ko call kar raha // huga wo waha par bhee kaam nahe kar payega
/*F19, int = (int) + (int...a) ,AP */ class F_019_Temp { void show(int a){ System.out.println("int method"); } void show(int...a){ System.out.println("varargs m/t "); } public static void main(String[] args) { F_019_Temp t = new F_019_Temp(); t.show(10); // single works } } // op = int method // the varags allows the m/t to accepts zero or multiple aruguments. // before vaarrgs either we use overloading method or take an array as the m/t parameters // but it was not considerd good because it leads to the maintenance problem. // if we dont know how many agruments we will have to padd in the method , // arags is the best approch
/*F20, int(10,20,30) = (int) + (int...a), AP */ class F_020_Temp { void show(int a){ System.out.println("int method"); } void show(int...a){ System.out.println("varargs m/t "); } public static void main(String[] args) { F_020_Temp t = new F_020_Temp(); t.show(10,20,30); // multiple also works } } // op = varargs m/t ,
/*F21, Null = (int) + (int...a), AP */ class F_021_Test { void show(int a){ System.out.println("int method"); } void show(int...a){ System.out.println("varargs m/t "); } public static void main(String[] args) { F_021_Test t = new F_021_Test(); t.show(); } } // op = varargs m/t // in general ,varargs get leaset priority i.e. if no other m/t matched , // then only varargs m/t will get the change because int came in 1.0 version and // vargs came in 1.5 version...
/*F22, int = (float) + (double) + (long) */ class EClass { void show(float a){ System.out.println("int to float"); } void show(double a){ System.out.println("int to double"); } void show(long a){ System.out.println("int to long"); } public static void main(String[] args) { EClass t = new EClass(); t.show(10); } } // int to long
/*F23, int = (float) + (double) */ class EClass { void show(float a){ System.out.println("int to float"); } void show(double a){ System.out.println("int to double"); } public static void main(String[] args) { EClass t = new EClass(); t.show(10); } } // int to float
/*F24, int = (float) */ class EClass { void show(float a){ System.out.println("int to float"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { EClass t = new EClass(); t.show(10); } } // int to float
/*F25, int = (double) */ class EClass { void show(double a){ System.out.println("int method"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { EClass t = new EClass(); t.show(10); } } // int method
/*F26, int = (long) */ class EClass { void show(long a){ System.out.println("int method"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { EClass t = new EClass(); t.show(10); } } // int method
/*F27, byte, short, char = (int) + (string) */ class EClass { void show(int a){ System.out.println("int method"); } void show(String a){ System.out.println("String method"); } public static void main(String[] args) { byte b = 101; short s = 200; EClass t = new EClass(); t.show('a'); t.show(b); t.show(s); } } // int method // int method // int method
/*F28, Main Method ol */ class EClass { public static void main(String[] args) { System.out.println("1"); EClass t = new EClass(); t.main(new int[]{20}); } public static void main(int[] args) { System.out.println("2"); } } // 1 // 2 // GPT // 1. New Int[]{20}: This Creates A New Array Of Integers With A Single Element 20. // 2. The Syntax New Int[]{20} Is Creating An Anonymous Array With One Element, Which Is 20. // 3. This Array Is Passed As An Argument To The Main(Int[] Args) Method.
Home PREVIOUS NEXT