In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to beoverloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism.
If you have never used a language that allows the overloading of methods, then the concept may seem strange at first. But as you will see, method overloading is one of Java’s most exciting and useful features
Method overloading is the process of defining more than one method within a class with the same name but not the arguments (formal parameters) and return type. When you define a method with the same name more than one time in a class , then the methods which are defined with the same name should not contain same arguments which we are passing . That is , if we declare two methods with the same name , then the , JVM will search for a particular method according to the parameters we are passing . In simple words, the method should be same but not the signature . Signature of the method matters in overloading .
Here , is the simple example for the method overloading .
PROGRAM ON METHOD OVERLOADING :
CLICK ON THE PROGRAM TO ENLARGE :
As you can see that test() method is overloading four times with the same name but with different parameters . when the method test() is called by the object ‘t’ of ‘TestOverloading’ type then it jumps to TestOverloading type (class) and searches for test() method with no parameters and executes . similarly , when the method test(int x, int y) is called by the object ‘t’ of ‘TestOverloading ‘ type then it jumps to TestOverloading class and searches for the method test() with two integer arguments. This is the way, the JVM will ensure the signature of the method .
By the above explanation , you have got a clear idea what exactly the method overloading is . That is , Method overloading is the process of defining more than one method within a class with the same name but not the arguments (formal parameters) and return type. But you will face one doubt in your programming of method overloading , that,
***Is it compulsory to pass the same argument type which we have defined inside a method???????
Well the answer is “yes” it is possible to do so.
Don’t worry about that i will give a clear picture of that doubt . Here, suppose in a class you have declared a method with one parameter as double and you are trying to pass an ‘int’ type argument to that method , now the question is will the JVM takes that int type argument and passes to that double type argument method?????
“yes” the JVM will pass that int type to double type argument , by converting the value implicitly , that is type casting . I have already explained you what exactly the type casting is .
NOW THE PROGRAM IS :
click on the program to enlarge
CAN WE OVERLOAD THE MAIN() METHOD IN JAVA LANGUAGE ????
RULE IS RULE FOR EVERYONE . EVERY METHOD CAN BE OVERLOADED EVEN THE HEAD OF THE PROGRAM MAIN() METHOD ALSO . LET’S SEE HOW?
CLICK ON THE PROGRAM TO ENLARGE
ANY DOUBTS REGARDING CORE JAVA JUST PING MY BLOG. I ALWAYS STICKS TO MY BLOG WITH ANSWERS. THANK YOU 🙂