Method Overloading

Method Overloading, if a class has multiple methods with the same name but different parameters, it is called Method Overloading.

Overloading allows us to use functions or methods with the same name, but different arguments.

It is a way through which Java supports polymorphism. Polymorphism deals with multiple form, and it is a concept of object-oriented programming.

By changing a several arguments or changing data types of argument method overloading can be done. In method overloading, it increases the readability of the program.

The java compiler distinguishes amongst overloaded methods based on their parameter list: the methods return type is not considered when overloaded. Thus the two method declarations that follow are not overloaded because they have the same signature; they will generate a compiler error.

public void myPrintMethod( int someValue) {}

and

public int myPrintMethod( int someOtherValue ) {}

The compiler highlights the second method declaration and outputs the message:

myPrintMethod already defined. In other words, the compiler cannot distinguish between the two methods.

Different ways of method overloading.
  1. The Different data type of arguments
  2. Different number of arguments
Method overloading by changing data type of arguments.
Example

In this example, we have two sum() methods that take integer and float type arguments.

Notice that in the same class we have two methods with the same name but different types of parameters.

		class Calculate
		{
		  void sum (int X, int Y)
		{
		    System.out.println("sum is"+(X+Y)) ;
		  }
		  void sum (float X, float Y)
		  {
		    System.out.println("sum is"+(X+Y));
		  }
		  Public static void main (String[] args)
		  {
		    Calculate  cal = new Calculate();
		    cal.sum (5,5);      //sum(int X, int Y) is method is called.
		    cal.sum (5.6f, 2.3f); //sum(float X, float Y) is called.
		  }
		}
Output

Sum is 10

Sum is 7.9

Here sum() method is overloaded two times. First, takes two integer arguments and second, takes two float arguments.

Method overloading by changing no. of argument.
Example:

Here, we have two methods

		class Test
		{
		  void multiply(int X, int Y)
		  {
		    System.out.println("Result is"+(X*Y)) ;
		  }
		  void multiply(int X, int Y,int Z)
		  {
		    System.out.println("Result is"+(X*Y*Z));
		  }
		  public static void main(String[] args)
		  {
		    Test  ar = new Test();
		    ar.multiply(5,5);   //multiply(int X, int Y) is method is called
		    ar.multiply(4,3,2);   //multiply(int X, int Y,int Z) is called
		  }
		}
Output

Result is 25

Result is 24

Here multiply() method is overloaded twice. The first method takes two arguments, and the second method takes three arguments.

Example of Method overloading with type promotion.

In this example, we are doing the same and calling a function that takes one integer and second long type argument.

		class Test2
		{
		 void sum(int X,long Y)
		 {
		  System.out.println("Sum is"+(X+Y)) ;
		 }
		 void sum(int X, int Y, int Z)
		 {
		  System.out.println("Sum is"+(X+Y+Z));
		 }
		 public static void main (String[] args)
		 {
		  Test2  ar = new Test2();
		  ar.sum(7,3);
		 }
		}
Output

Sum is 10

Overloading main Method
		class TestOverloading4{  
		public static void main(String[] args){System.out.println("main with String[]");}  
		public static void main(String args){System.out.println("main with String");}  
		public static void main(){System.out.println("main without args");}  
		}
Output

main with String[]

Copyright 1999- Ducat Creative, All rights reserved.