Вы находитесь на странице: 1из 9

Method Overloading in Java

Method Overloading is the ability to define more than one method with the same
name in a class but with different parameters. The compiler is able to distinguish
between the methods because of their method signatures.

method signature= method name + method parameter

Advantages of method overloading

 method overloading increases the readability of the program


 main advantage of method overloading is cleanliness of the program

Rules of method overloading

 we can overload a method by changing the signature of the method.


note:method signature comprises of number of arguments, type of arguments and order of
arguments.
 changing the return type of the method means no overloading because return type is not a
part of method signature.
 a method can be overloaded in a same class or a sub-class.

Different ways to overload methods in


Java

There are two ways to overload methods. They are:


1. by changing the number of arguments
2. by changing the data type of the parameter

1) Example of method overloading by changing the number of arguments

class Calculate{

void sum(int a, int b){

System.out.println("Sum of two numbers="+(a+b));

void sum(int a,int b,int c){

System.out.println("Sum ot three numbers="+(a+b+c));

public static void main(String args[]){

Calculate obj=new Calculate();

obj.sum(10,20);

obj.sum(10,20,30);

Output:

Sum of two numbers=30


Sum of three numbers=60
2) Example of method overloading by changing the data type of the parameter

class Calculate{

void sum(int a,int b){

System.out.println("Sum of two numbers="+(a+b));

void sum(double a, double b, int c){

System.out.println("Sum ot three numbers="+(a+b+c));

public static void main(String args[]){

Calculate obj=new Calculate();

obj.sum(10,20);

obj.sum(10.5, 20.5 ,30);

Output:

Sum of two numbers=30


Sum of three numbers=61.0

Why method overloading is not


possible by changing the return type of
method ?
In java it is not possible to overload method by changing the return type of the method
because there occurs ambiguity problem. Compiler is not able to distinguish between
the methods to be called and thus raise compile time error. Let's see an example:

class Calculate{

int sum(int a,int b){

return a+b;

double sum(int a, int b){

return a+b;

public static void main(String args[]){

Calculate obj=new Calculate();

obj.sum(10,20);

Output:
compile time error is generated
Calculate.java:4: sum(int,int) is already defined in Calculate
double sum(int a, int b){
^
1 error
Can we overload main() method ?

Yes, it is possible to overload main() method with the help of method overloading.
Let's see an example below through which it will be clear that we can overload main()
method.

class MainDemo{

public static void main(){

System.out.println("Duplicate main");

public static void main(String args[]){

System.out.println("Original main");

main();

Output:

Original main
Duplicate main
Method Overloading and Type
Promotion

Type promotion is an automatic type conversion from a "lesser" base type to a


"greater" one.Let us see the diagram given below and understand it.

It must be clear from the above diagram that byte can be promoted to short, int, long,
float or double. The short datatype can be promoted to int,long,float or double. The
char datatype can be promoted to int,long,float or double and so on.
One type is promoted to its greater type implicitlty it no matching datatype is
found.

1) Example of method overloading with Type Promotion

class TypePromotion{

void add(float a, float b){


System.out.println(a+b+" float");

void add(long a, long b){

System.out.println(a+b+" long");

public static void main(String args[]){

TypePromotion obj=new TypePromotion();

obj.add(21,21); //integer value is passed

Output:

42 long

In the above example integer value is passed through obj.add(21,21) which will be
implicitly promoted to its nearest bigger type i.e. long type.

2) Example of method overloading with Type Promotion

class TypePromotion{

void add(float a, float b){

System.out.println(a+b+" float");

void add(double a, double b){


System.out.println(a+b+" double");

public static void main(String args[]){

TypePromotion obj=new TypePromotion();

obj.add(21,21); //integer value is passed

Output:

42.0 float

In the above example integer value is passed through obj.add(21,21) which will be
implicitly promoted to its nearest bigger type i.e. float type. [note:nearest bigger type
of int is long which is not matched in the above program, therefore next bigger type is
matched and is promoted to float implicitly]

3) Example of method overloading with Type Promotion giving ambiguity

class TypePromotion{

void add(int a, long b){

System.out.println(" 1st method invoked");

void add(long a, int b){

System.out.println( " 2nd method invoked");

}
public static void main(String args[]){

TypePromotion obj=new TypePromotion();

obj.add(21,21);

Compile time error will be generated because one-type cannot be depromoted


to any type implicitly.
Output:
Compile time error
TypePromotion.java:13: reference to add is ambiguous, both method add(int,long)
in TypePromotion and method add(long,int) in TypePromotion match
obj.add(21,21);
^
1

Вам также может понравиться