1
0

Overloading of Methods in Java

In Java, it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations or parameter type or number of parameters are different. The concept is similar to constructor overloading discussed in the previous section. When two or more methods have the same name, the methods are said to be overloaded, and the process is referred to as method overloading.

Method overloading is one of the ways that Java implements polymorphism. If you have never used a language that allows the overloading of methods, then the concept may seem strange at first. But as you will see, method overloading is one of Java’s most exciting and useful features. 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. Thus, overloaded methods must differ in the type and/or the number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. Here is a simple example that illustrates method overloading:

Program to demonstrate method overloading in classes

class Rectangle
{
    double length;
    double width;
    void data(double a, double b)
    {
        length=a;
        width=b;
    }
    void data(double a) // overload data() for a single parameter
    {
        length=a;
        width=12;
    }
    void show()
    {
        System.out.println("Length of the Rectangle = "+length);
        System.out.println("Width of the Rectangle = "+width);
        System.out.println("Area of the Rectangle = "+length*width);
    }
}
class ClassReturn
{
    public static void main(String args[])
    {
        double result;
        Rectangle Rect1=new Rectangle();
        Rectangle Rect2=new Rectangle();
        Rect1.data(20, 30); // call to void data(double a, double b);
        Rect1.show();
        Rect2.data(20); // call to void data(double a);
        Rect2.show();
    }
}

Output:

Length of the Rectangle = 20.0
Width of the Rectangle = 30.0
Area of the Rectangle = 600.0
Length of the Rectangle = 20.0
Width of the Rectangle = 12.0
Area of the Rectangle = 240.0

As you can see, data() is overloaded two times. The first version takes two parameters the second takes one parameter. Note that return types do not play any role in method overloading. Overloaded methods must differ in the number or type of arguments.

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

Program for Automatic type conversions applied to method overloading.

class Rectangle
{
    double length;
    double width;
    void data()
    {
        length=20;
        width=10;
    }
    void data(double a, double b) //overload data() for a double parameter
    {
        length=a;
        width=b;
    }
    void data(double a) // overload data() for a single parameter
    {
        length=a;
        width=12;
    }
    void show()
    {
        System.out.println("Length of the Rectangle = "+length);
        System.out.println("Width of the Rectangle = "+width);
        System.out.println("Area of the Rectangle = "+length*width);
    }
}
class ClassReturn
{
    public static void main(String args[])
    {
        int var=20;
        // void result;
        Rectangle Rect1=new Rectangle();
        Rectangle Rect2=new Rectangle();
        Rectangle Rect3=new Rectangle();
        Rect1.data();
        Rect1.show();
        result=Rect2.data(12,5);
        System.out.println("Area of the Rectangle="+result);
        Rect3.data(var);
        Rect3.show();
    }
}

Output:

Length of the Rectangle = 20
Width of the Rectangle = 10
Area of the Rectangle = 200
Length of the Rectangle = 12
Width of the Rectangle = 5
Area of the Rectangle = 60
Length of the Rectangle = 20
Width of the Rectangle = 12
Area of the Rectangle = 240

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

Note-2: Method overloading supports polymorphism because it is one way that Java implements the “one interface, multiple methods” paradigm. To understand how, consider the following. In languages that do not support method overloading, each method must be given a unique name. However, frequently you will want to implement essentially the same method for different types of data. Consider the absolute value function. In languages that do not support overloading, there are usually three or more versions of this function, each with a slightly different name. For instance, in C, the function abs() returns the absolute value of an integer, labs() returns the absolute value of a long integer, and fabs() returns the absolute value of a floating-point value. Since C does not support overloading, each function has to have its own name, even though all three functions do essentially the same thing. This makes the situation more complex, conceptually, than it actually is. Although the underlying concept of each function is the same, you still have three names to remember. This situation does not occur in Java, because each absolute value method can use the same name. Indeed, Java’s standard class library includes an absolute value method, called abs( ). This method is overloaded by Java’s Math class to handle all numeric types. Java determines which version of abs() to call based on the type of argument. The value of overloading is that it allows related methods to be accessed by use of a common name. Thus, the name abs represent the general action that is being performed. It is left to the compiler to choose the right specific version for a particular circumstance. You, the programmer, need only remember the general operation being performed. Through the application of polymorphism, several names have been reduced to one. Although this example is fairly simple, if you expand the concept, you can see how overloading can help you manage greater complexity.

When you overload a method, each version of that method can perform any activity you desire. There is no rule stating that overloaded methods must relate to one another. However, from a stylistic point of view, method overloading implies a relationship. Thus, while you can use the same name to overload unrelated methods, you should not. For example, you could use the name sqr to create methods that return the square of an integer and the square root of a floating-point value. But these two operations are fundamentally different. Applying method overloading in this manner defeats its original purpose. In practice, you should only overload closely related operations.

Leave a Reply