In this post, I show you why method overloading is used in java or What happens if there is no method overloading in java.
Method Overloading Definition:
When two or more method within the same class and have same method name but different data type or number of arguments are said to be Method overloading
2) By changing Data type.
We would have to use this without Method overloading:
Even worse thing we have you used:
Advantages of Method Overloading is:
#)Only for the cleanliness of code and for better understanding.
OUTPUT: int
If more than one method declares in a class and both accessible and applicable to a method invocation(an argument that we are passing), it is necessary the most specific method is chosen.
In simple words, it will choose int first coz of the deepest class as given below hierarchy.
In Above Example if you remove int it will print long if you remove long it will print double and so on.
For Tricky Method, Overloading Question CLICK HERE
Method Overloading Definition:
When two or more method within the same class and have same method name but different data type or number of arguments are said to be Method overloading
OR
Method Overloading in Java is the ability to create multiple methods with the same name, but with different parameters or a number of arguments.
Rules of Method Overloading or How we achieved Method Overloading
1)By changing a number of arguments.2) By changing Data type.
Life without Method Overloading in Java OR What if we didn't have Method Overloading in Java.
Take a look at System.out.println() method think what will happen if println() method was not Overloaded with different data types, instead of we have to remember different function to print different type.
Instead of using multiple Method overloading of println() :
System.out.println("string"); System.out.println(15); System.out.println('z'); System.out.println(10.8); System.out.println(false);
We would have to use this without Method overloading:
System.out.printlnString("string"); System.out.printlnInt(15); System.out.printlnChar('z'); System.out.printlnFloat (10.8); System.out.printlnBoolean (false);
Even worse thing we have you used:
System.out.println1("String"); System.out.println2(5); System.out.println3('c');
Advantages of Method Overloading is:
#)Only for the cleanliness of code and for better understanding.
Tricky Method Overloading Question in java?
public class MethodOverloading { public static void overloadingMethod(int number) { System.out.println("int"); } public static void overloadingMethod(long number) { System.out.println("long"); } public static void overloadingMethod(double number) { System.out.println("double"); } public static void overloadingMethod(Integer number) { System.out.println("Integer"); } public static void overloadingMethod(Number number) { System.out.println("Number"); } public static void main(String[] args) { overloadingMethod(10); } }
OUTPUT: int
If more than one method declares in a class and both accessible and applicable to a method invocation(an argument that we are passing), it is necessary the most specific method is chosen.
In simple words, it will choose int first coz of the deepest class as given below hierarchy.
Hierarchy |
In Above Example if you remove int it will print long if you remove long it will print double and so on.
For Tricky Method, Overloading Question CLICK HERE
Post a Comment