codescracker


java

Java Method Overloading



« Previous Tutorial Next Tutorial »


In Java, you can define two or more methods in the same class that share the same name, as long as their parameter declarations are dissimilar. When this is the case, then the methods are said to be overloaded, and the process is referred to as the method overloading.

Method overloading is one of the way that Java supports polymorphism. If you have never used a language that allows method overloading, then the concept may seem strange for you. But you will see, method overloading is one of the Java's most exciting and useful features.

Java Method Overloading Rule

When an overloaded method is invoked, Java uses the type and/or the number of arguments as its guide to determine which version of the overloaded method to actually call. Therefore, overloaded methods must differ in the type and/or the number of their parameters. While overloaded methods may have dissimilar return types, the return type alone is insufficient to distinguish the two versions of method. Whenever Java encounters a call to an overloaded method, it is simply executes the version of the method of which the parameters match the arguments used in the call.

Java Method Overloading Example

Following is a simple example program that illustrates the method overloading in Java:

/* Java Program Example - Java Method Overloading
 * This program demonstrate the method overloading
 * concept of Java
 */
   
class OverloadDemo
{
    void test()
    {
        System.out.println("No parameters");
    }
    
    /* Overload the test for one integer parameter */
    void test(int a)
    {
        System.out.println("a : " + a);
    }
    
    /* Overload the test for two integer parameters */
    void test(int a, int b)
    {
        System.out.println("a is " + a + "\nb is " + b);
    }
    
    /* Overload the test for a double parameter */
    double test(double a)
    {
        System.out.println("double a : " + a);
        return a*a;
    }
}

class Overload
{
    public static void main(String args[])
    {
	
        OverloadDemo ob = new OverloadDemo();
        double result;
        
        /* all the version of method test() */
        ob.test();
        ob.test(100);
        ob.test(100, 200);
        
        result = ob.test(123.25);
        
        System.out.println("Result of ob.test(123.25) is " + result);
		
    }
}

When the above Java program is compile and executed, it will produce the following output:

java methods overloading

As you can see, test() method is overloaded four times as follows:

  1. test() - takes no parameters
  2. test(int a) - takes one integer parameter
  3. test(int a, int b) - takes two integer parameters
  4. test(double a) - takes one double parameter

The fact that the fourth version of the test() also returns a value is of no consequence relative to overloading, since return types don't play a role in overload resolution.

Java Method Overloading Program

When an overloaded method is called, Java looks for a match between the arguments used to call the method and method's parameters. However, this match need not always be exact. In several cases, Java's automatic type conversions can act a role in overload resolution. For example, consider the following program :

/* Java Program Example - Java Method Overloading
 * Automatic type conversion apply to overloading
 */

class OverloadDemo
{
    void test()
    {
        System.out.println("No parameters");
    }
    
    /* overload test for the two integer parameters */
    void test(int a, int b)
    {
        System.out.println("a is " + a + "\nb is " + b);
    }
    
    /* overload test for a double parameter */
    void test(double a)
    {
        System.out.println("Inside test(double) a : " + a);
    }
}

class Overload
{
    public static void main(String args[])
    {
	
        OverloadDemo ob = new OverloadDemo();
        int i = 88;
        
        ob.test();
        ob.test(100, 200);
        
        ob.test(i);   // this will invoke test(double)
        ob.test(123.2);   // this will invoke test(double)
		
    }
}

When the above Java program is compile and executed, it will produce the following output:

java methods overloading example

As you can see, this version of the OverloadDemo does not define test(int). Therefore, when test() is called with an integer argument inside the Overload, no matching method is found. However, Java can automatically convert an integer into double, and this conversion can be used to resolve the call. Therefore, after test(int) had been defined, it would have been called rather. Java will employ its automatic type conversions only if no exact match is found.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test