The overloaded method must have a different parameter list. Methods cannot be overloaded based on different modifiers or return value types.
PackageWelcome; Public classtestmethodoverloading { Public Static voidMain (string[] args) {System.out.println ("The maximum between 3 and 4 is" + MAX (3, 4));//call the max (int, int) methodSystem.out.println ("The maximum between 3.0 and 5.4 is" + max (3.0, 5.4));//Call the Max (double, double) methodSystem.out.println ("The maximum between 3.0, 5.4 and 10.14 is" + max (3.0, 5.4, 10.14));//Call the Max (double, double, double) method } Public Static intMaxintNUM1,intnum2) { if(Num1 >num2) { returnNUM1; }Else{ returnnum2; } } Public Static DoubleMaxDoubleNUM1,Doublenum2) { if(Num1 >num2) { returnNUM1; }Else{ returnnum2; } } Public Static DoubleMaxDoubleNUM1,DoubleNUM2,Doublenum3) { returnMax (Max (num1,num2), num3); }}
PackageWelcome;/** Demo Ambiguous call * When a method is called with one or more possible matches, the compiler cannot determine which match is more precise, which is called ambiguous invocation. * Ambiguous calls throw compilation errors*/ Public classambiguousoverloading { Public Static voidMain (string[] args) {System.out.println (max ());//max (int, double) and Max (double, int) are all likely to match Max, because neither of these methods is more precise than who}//so this call is ambiguous and will throw a compilation error Public Static DoubleMaxintNUM1,Doublenum2) { if(Num1 >num2) { returnNUM1; }Else{ returnnum2; } } Public Static DoubleMaxDoubleNUM1,intnum2) { if(Num1 >num2) { returnNUM1; }Else{ returnnum2; } }}
Java Learning Notes Method overloading