Method Overloading allows multiple methods to have same name if the parameter list inside parenthesis are different. The parameter list can differ in number of parameter, sequence of data type or type of parameter.
Formal Definition: “If a class has multiple methods by same name but different arguments, it is known as Method Overloading”.
Table of Contents
Overloading is what, when we use same thing again and again for different purposes, for example in figure below a Donkey is overloaded, same is the case with Method Overloading,
Need Of Method Overloading:
Firstly let us discuss how Method overloading comes into picture and makes our task much easier.
Let us take an example, suppose we have to add two Integers, then we define a method addTwoIntegers() (which is returning an int value) as shown:
int addTwoIntegers(int a, int b) { int c; c=a+b; return c; }
Now, Requirement comes to add three integers, so now we define another method addThreeIntegers() with three arguments as shown:
int addThreeIntegers(int a, int b, int c) { int d; d=a+b+c; return d; }
Now if requirement is to add four integers, then we will again write a method addfourIntegers(), which will make our task of remembering much difficult that which method is for what.
Method overloading is the concept of using same method name, but passing different arguments, which will make our task of remembering much easier. Let us discuss it by taking same example but using method overloading , we will use one method add(), and use it for different tasks.
For adding two Integers we will use same Method add() with two arguments as shown:
int add(int a, int b) { int c; c=a+b; return c; }
For adding three Integers we will use same Method add() with three arguments as shown:
int add(int a, int b, int c) { int d; d=a+b+c; return d; }
And likewise we can change the number of Arguments in the method to perform different tasks based on the requirement that comes.
Important Note: If the methods have different return type then they are not considered as overloaded methods and hence error message is generated by compiler. The reason behind this is that it is not possible for the compiler to determine which version of the method to call.
Method Overloading can be done by following types:
Example for overloading a method by changing the number of parameters:
In this example we have defined two overloaded methods, first method adds two Integer values and other adds four Integer values:
public class MethodOverloading { void add(int a,int b) { System.out.println(a+b); } void add(int a, int b, int c, int d) { System.out.println(a+b+c+d); } public static void main(String args[]){ MethodOverloading obj=new MethodOverloading(); obj.add(10,100); obj.add(25,35,45,55); } }
Output:
110 160
Example for overloading a method by changing the type of parameters:
In this example we have defined two overloaded methods, first method adds two Integers and other adds two float values:
public class MethodOverloading { void add(int a,int b) { System.out.println(a+b); } void add(float a,float b) { System.out.println(a+b); } public static void main(String args[]){ MethodOverloading obj=new MethodOverloading(); obj.add(15f,15f); obj.add(25,25); } }
Output:
30.0 50
Lets discuss it with example; it will give you Compile time error as shown in comment segment
public class ArrayListMethods { int add(int a,int b) { int c; c=a+b; return c; } //Change return type but method has same number and type of parameter double add(int a,int b) // Giving Compile time error of Duplicate method { double c; c=a+b; return c; } public static void main(String args[]){ ArrayListMethods obj=new ArrayListMethods(); System.out.println(add(20,20)); } }
Important Interview Question on Method Overloading:
Q: Can we have main() method overloaded?
Ans: Yes, we can have more than one main method having different number and types of arguments.
Lets discuss it by taking an example, in this we have two main methods, 1st main method behave as a normal method and two integers are passed in that, which is returning its sum, and 2nd main method is called by Java Virtual Machine (JVM) during start , and which is calling the above main method as shown:
public class MethodOverloading { public static void main(int a, int b){ System.out.println(a+b); } public static void main(String args[]){ System.out.println("We are in MAIN Method"); main(5,10); } }
Output:
We are in MAIN Method 15
Premium Project Source Code:
Leave a Reply