Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Method overloading

In a class when there are two or more methods with the same name but different types, numbers & sequence of parameters, it is known as method overloading.
And the methods are called overloaded methods.
Java invokes the correct method among overloaded methods by checking the number, type & sequence of the parameters. That is, the method whose parameters match with the arguments passed by caller method is invoked.
Return type alone is not sufficient to overload the methods.
It is the best example of polymorphism.

Example

public class MethodOverloadDemo 
{
     public static void addition(int a,int b)
     {
         int sum;
         sum = a+b;
         System.out.println("Addition of two int values = "+sum);
     }
	
     public static void addition(double a,double b) 
     {
         double sum;
         sum = a+b;
         System.out.println("Addition of two double values  = "+sum);
     }
	
     public static void main(String args[])
     {
         addition(5,6);
         addition(12.3,15.4);
     }
}

Output

Addition of two int values = 11
Addition of two double values = 27.700000000000003

For the better understanding of the above program look at the diagram shown below:

Java Programming Language Method Overloading Working

Example No. 2

public class MethodOverloadDemo2 
{
    public void addition(int a,int b) 
    {
        System.out.println("Addition of two integer values :: "+(a+b));
    }
    
    public void addition() 
    {
        System.out.println("Parameters not available for addition");
        System.out.println("------------------------------------");
    }
	
    public void addition(float p,float q) 
    {
        System.out.println("Addition of two float values :: "+(p+q));
        System.out.println("------------------------------------");
    }
	
    public void addition(double l,double m) 
    {
        System.out.println("Addition of two double values :: "+(l+m));
        System.out.println("------------------------------------");
    }
	
    public void addition(String n1,String n2,String n3) 
    {
        System.out.println("Addition of three String values :: "+(n1+n2+n3));
        System.out.println("------------------------------------");
    }
	
    public static void main(String args[])
    {
        MethodOverloadDemo2 d2 = new MethodOverloadDemo2();
       
        d2.addition(5.5, 6.6);
        d2.addition(3.3f, 1.2f);
        d2.addition();
        d2.addition("All ", "Is ", "Well ");
        d2.addition(10, 20);
    }	
}

Output

Addition of two double values :: 12.1
------------------------------------
Addition of two float values :: 4.5
------------------------------------
Parameters not available for addition
------------------------------------
Addition of three String values :: All Is Well
------------------------------------
Addition of two integer values :: 30

Why method overloading?

It is better to assign a name to a method, similar to the task that it performs. But sometimes two or more methods work on similar tasks. Therefore it becomes very difficult to assign a distinct name related to those similar tasks for each & every method.
To overcome this problem, Java introduced a concept called method overloading.

For better understanding, consider two or more methods performing a task related to addition. So it becomes difficult to assign a distinct name to each & every method related to addition.

What if passed arguments & method’s parameters do not match?

If method parameters & passed arguments do not match exactly, passed arguments are promoted into method parameters type automatically, if possible.

If not possible, a compile-time error occurs.
Compile time error also occurs if more than one match is available.

Example

public class MethodOverloadDemo3 
{
    public void display(double a,double b,double c) 
    {
        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);
        System.out.println("------------------------");
    }
	
    public void display(float e) 
    {
        System.out.println("e = "+e);
    }
	
    public void display(double i,double j) 
    {
        System.out.println("i = "+i);
        System.out.println("j = "+j);
        System.out.println("------------------------");
    }
	
    public static void main(String args[])
    {
        MethodOverloadDemo3 d3 = new MethodOverloadDemo3();
        d3.display(10, 15);
        d3.display(4.6f, 5.6f, 6.6f);
        d3.display(5);		
    }
}

Output

i = 10.0
j = 15.0
------------------------
a = 4.599999904632568
b = 5.599999904632568
c = 6.599999904632568
------------------------
e = 5.0

Java Programming Language Way of Method Overloading Working

Similarly, it happens with every call to check method.

 

Leave a reply