METHOD OVERLOADING IN JAVA

Method Overloading in Java is a mechanism in which different methods are allowed to have the same name if it can be distinguished by their number and type of arguments. We can also say a method is said to be overloaded if several method declarations are specified for a single method name is the same scope.

Table of Contents

[hide]
  • What is Method Overloading in Java
  • Need for Method Overloading
  • Different ways of Method Overloading
  • Type Promotion with Method Overloading

What is Method Overloading in Java

Method overloading in Java implements polymorphism and also reduces number of comparisons in a program and thereby makes the program run faster.

A method name having several definitions in the same scope that are differentiable by the number of types of their arguments , is said to be an overloaded methods.

Example :

void square(int a, int b);
void square(float x, float y);

That is, square() taking two int arguments is different from square() taking two float arguments. This is known as Method Overloading.

Now let us learn about why there is a need for Method Overloading in the next section.

Need for Method Overloading

In real life scenarios same behaviour of same object may differ in different situations. Then why can't this happen with methods of a class ? After all, it is a class that implements OOP in practice. Therefore, in order to simulate real-world objects in programming, it is necessary to have method overloaded.

Now you must be thinking when we can define different methods for the different situations, then why should we repeat the same method name again and again.

In the above situation where we have different method name, we as a programmer decide upon which method should be executed. Now in the other case where we have two methods having same name, in this case compiler automatically decides which method should get executed.

It will not only reduce the code by reducing the number of if-else but also make the code execute faster as so many comparisons are eliminated. Also it increases the redability of a program.

Different ways of Method Overloading

There are three ways to overload a method :

  • 1. By changing number of parameters.
  • 2. By changing Data type of parameters.
  • 3. By changing Sequence of Data type of parameters.

Let us learn more about these in detail in the next section.

By changing number of parameters

Let us consider an example where we will have two methods with same name but with different number of parameters.

Example : Method Overloading in Java with different number of parameters

copyimage
//Java Program to demonstrate the use of method overloading in Java
class OverloadingExample {
  public int maximum(int num1, int num2) {
	return num1>num2?num1:num2;
  }

  public int maximum(int num1, int num2, int num3) {
	if (num1 >= num2 && num1 >= num3)
	    return num1;
	else if (num2 >= num1 && num2 >= num3)
	    return num2;
	else
	    return num3;
    }
}
class Codebator  { 
public static void main(String[] args) {
	OverloadingExample obj=new OverloadingExample();
	int maxoftwo = obj.maximum(30,10);
	int maxofthree = obj.maximum(20,50,30);
	System.out.println("maximum of two number is : "+ maxoftwo);
	System.out.println("maximum of three number is : "+ maxofthree);
  }
}

OUTPUT :

maximum of two number is : 30maximum of three number is : 50 

In the above example you can see that we have both the methods with the same name. We are passing two arguments in the first method while the second method have three arguments. Compiler automatically decides which method should get executed based on the number of arguments passed.

In Java, method overloading is not possible only by changing the method's return type.

By changing Data type of parameters

We have already seen method overloading by changing the number of parameters, now let us see an example where we will change the data type of the arguments.

Example : Method Overloading in Java by changing Data type of parameters

copyimage
//Java Program to demonstrate the use of method overloading in Java
class OverloadingExample {
  public static int maximum(int num1, int num2) {
     return num1>num2?num1:num2;
  }

  public static double maximum(double num1, double num2) {
     return num1>num2?num1:num2;
  }
}
class Codebator  { 
  public static void main(String[] args) {
     System.out.println(OverloadingExample.maximum(20, 50));
     System.out.println(OverloadingExample.maximum(20.4, 30.6));
  }
}

OUTPUT :

5030.6 

In the above example you can see that we have both the methods with the same name. We are passing two arguments in the first method with int data type while the second method have two arguments with double data type. Compiler automatically decides which method should get executed based on Data type of parameters

Note since the methods are static we are directly able to access the method with class name rather than creating an object of a class and then accessing it.

By changing Sequence of Data type of parameters

In this type of method overloading we can have two methods with same name and parameters but the order of parameters is different

Example : Method Overloading in Java by changing Sequence of Data type of parameters

copyimage
//Java Program to demonstrate the use of method overloading in Java
class OverloadingExample {

  public static void employee(String name,  int id){ 
      System.out.println("Company Name :"+ name);
      System.out.println("Company Id :"+ id); 
  } 

  public static void employee(int id, String name){ 
      System.out.println("Company Id 2 :"+ id); 
      System.out.println("Company Name 2 :"+ name);
  } 
}
class Codebator  { 
public static void main(String[] args) {
   OverloadingExample.employee("Google", 50434);
   OverloadingExample.employee(30434,"Apple");
}
}

OUTPUT :

Company Name :GoogleCompany Id :50434 Company Id 2 :30434 Company Name 2 :Apple 

Type Promotion with Method Overloading

If a smaller size type of data is promoted to the larger size type of data than this is called type promotion, for example: byte data type can be promoted to short, a short data type can be promoted to int, big, double etc.

Example : Type Promotion with Method Overloading

copyimage
//Java Program to demonstrate the use of method overloading in Java
class OverloadingExample {
  void maximum(int num1, double num2) {
  System.out.println(num1>num2?num1:num2);
}

void maximum(int num1, int num2, int num3) {
  if (num1 >= num2 && num1 >= num3)
      System.out.println(num1);
  else if (num2 >= num1 && num2 >= num3)
      System.out.println(num2);
  else
      System.out.println(num3);
  }
}
class Codebator  { 
public static void main(String[] args) {
	OverloadingExample obj=new OverloadingExample();
	obj.maximum(30,10); // now second int literal will be promoted to double  
	obj.maximum(20,50,30);
   }
}

OUTPUT :

30.050 

We have taken the same example as above where now we will change the data type of the second literal i.e int to double. You will see the output as double since int type is promoted to double type.

Type Promotion Table

On the left side, the data type can be converted to any of the data type on the right side.

  • byte → short → int → long
  • short → int → long
  • int → long → float → double
  • float → double
  • flong → float → double

Is it possible to overload Java main() Method ?

Yes it is possible to overload Java main() method. You can have any number of main method in your class but JVM call main() method which receives String array as arguments.

Example : Overloading Java main() Method

copyimage
class OverloadingExample{   
    public static void main(){
    System.out.println("This is the overloaded main method");
    } 
}
public class Codebator {
    public static void main(String[] args){
    OverloadingExample obj=new OverloadingExample();
    obj.main();
    }
}  

OUTPUT :

This is the overloaded main method

Hope you have understood the concept of Method Overloading in Java. Kindly share with you friends.