Method Overloading in Java

Method Overloading in Java


Method overloading is when a class has a number of methods with same name but their parameters are different and there are different numbers of parameters. For example the user wants to add three numbers in one method and two numbers in a different function. Then it will be confusing to use a (int, int, int) and b (int, int). Rather to use a (int, int, int) and a (int, int) will be more readable because the names of the functions will be same. This is called method or function overloading.


Advantage of method overloading:

Method overloading is used because it increases the readability of the program in Object oriented programming.


Different ways to overload the method:

In Java programming language there are two different ways to overload methods. The following are the two different ways to overload methods in Java:

  1. By changing number of arguments
  2. By changing the data type

If we change the return type of the method then it will not be possible to overload method in Java programming language.


Example of Method overloading by changing number of arguments in Java:

Consider the following example in which we have created two methods that will be overloaded:

CODE:

class Addition {

void add (int x, int y) {

System. out. println (x + y);

}

void add (int x, int y, int z) {

System. out. println (x + y + z);

}

public static void main (string args []) {

Addition obj = new Addition ();

obj. add (2, 3);

obj. add (2, 3, 4);

}

}

OUTPUT:

5

9

In the above example two function of same class are having same names but their number of arguments or parameters are different. We called both of the functions with same object. The program successfully adds two numbers in the first method and three numbers in the second method.


Example of method overloading by changing data type of argument:

Consider the following example in which we have two methods of same name but the data type of the arguments of both methods is different. In the example below the first example has arguments of integer data type and the second method has arguments of float data type:

CODE:

class Addition {

void add (int x, int y) {

system. out. println (a + y);

}

void add (double x, double y) {

system. out. println (x + y);

}

public static void main (string args []) {

Addition obj = new Addition ();

obj. add (2, 3);

obj. add (2.3, 3.3);

}

}

OUTPUT:

5

5.6

In the above example two functions having same name are declared but their arguments are of different data type. The first function has arguments of integer data type and the second function has arguments of float data type. The addition of the numbers either float or integer is successfully done.

Q. Why method overloading is not possible by changing the return type of method?

It is not possible in Java programming language to overload methods by changing their return type because ambiguity can be created. The Java cannot compare the two methods when they are called. Consider the following example in which we have tried to perform method overloading with methods of same name but different return type:

CODE:

class Addition {

int add (int x, int y) {

System. out. println (x + y);

}

double add (int x, int y) {

System. out. println (x + y);

}

public static void main (string args []) {

Addition obj = new Addition ();

int result=obj. add (2, 3);

}

}

OUTPUT:

Java cannot compile the program because it does not know which add () method should be called.

In the above example a compile error will be generated on the line int result=obj. add (2, 3).


Can we overload main () method?

The main () method in Java programming language can be overloaded. In a class in Java programming language we have a number of main methods by method overloading. Consider the following example in which we have overloaded the main method in Java programming language:

CODE:

class OverloadingMain {

public static void main (int x) {

system. out. println (x);

}

public static void main (string args []) {

system. out. println (“The main () method is invoked”);

main (5);

}

}

OUTPUT:

The main () method is invoked

10

In the above example the main () method is overloaded and we printed a value that is passed to the overloaded main function. The message is “The main () method is invoked” is printed by the original main () method.


Method Overloading and Type promotion:

In method overloading one data type can be promoted to another data type, this will be done implicitly and when no matching data type is found. Implicit type promotion is performed automatically by the compiler. The variables in an expression must be of same data type. If the data types of operands are different, the value with lower data types is converted into higher data type.

Consider the following diagram to understand this concept:

consider-the-following-diagram-to-understand-this-concept

From the above diagram we can conclude that a byte can be converted to short, int, long, float, and double. The short data type can be converted to int, long, float and double. The char data type can be converted to int, long, float and double.


Example of method overloading with type promotion:

Consider the following example in which we have overloaded methods with type promotion:

class OverloadingAddition {

void add (int x, float y) {

system. out. println (x + y);

}

void add (int x, int y, int z) {

system. out. println (x + y + z);

}

public static void main (string args []) {

OverloadingAddition obj = new OverloadingAddition ();

obj. add (3, 5);

obj. add (2, 3, 4);

}

}

OUTPUT:

8

9

In the above example the class has two methods. The method one has two arguments of different data types. In this method type promotion will be performed implicitly.


Example of method overloading with type promotion if match is found:

In Java programming language if the arguments are of same data type then type promotion will not be performed.

Consider the following example in which we have demonstrated that if the arguments are of same data type then type promotion will not be performed:

CODE:

class Addition {

void add (int x, int y) {

System. out. println (“int arg method () in invoked”);

}

void add (long x, long y) {

System. out. println (“long arg method () in invoked”);

}

public static void main (string args []) {

Addition obj = new Addition ();

obj. add (2, 3);

}

}

OUTPUT:

int arg method () in invoked

In the above example type promotion is not performed because of the matching data type of arguments. The function or method “void add (int x, int y)” will be invoked because the values that are passed to the method are int type and are matched. If the values passed will be of long data type then the second method that is “void add (long x, long y)” will be invoked.


Example of method overloading with type promotion in case of ambiguity:

In Java programming language if there are no arguments of same data type in methods and each method of the program is promoting the similar number of arguments. Then this will create ambiguity.

Consider the following example in which we have demonstrated this:

CODE:

class Addition {

void add (int x, long y) {

System. out. println (“c arg method () in invoked”);

}

void add (long x, int y) {

System. out. println (“d arg method () in invoked”);

}

public static void main (string args []) {

Addition obj = new Addition ();

obj. add (2, 3);

}

}

OUTPUT:

Compiler time error

In the above example, the compiler will not understand that which method should be called and hence an ambiguity will be created and compiler will generate an error.

One data type cannot not be de promoted to any other data type implicitly.

For example double data type cannot be de promoted to float data type implicitly.