Java Overloading

Method overloading is one of the ways that Java supports polymorphism.

Method Overloading

The methods are said to be overloaded if two or more methods within the same class that share the same name differs in their parameter declarations and the process is referred to as method overloading.

When an overloaded method is invoked, Java uses the type and/or 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 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:

// Demonstrate method overloading. 
class OverloadMethodDemo {
    void print() {
        System.out.println("No parameters");
    } 
    
    // Overload print for one integer parameter.
    void print(int a) {
        System.out.println("a: " + a);
    } 
    
    // Overload print for two integer parameters.
    void print(int a, int b) {
        System.out.println("a and b: " + a + " " + b);
    } 
    
    // overload print for a double parameter
    double print(double a) {
        System.out.println("double a: " + a);
        return a * a;
    }
}

class Overload {
    public static void main(String args[]) {
        OverloadMethodDemo ob = new OverloadMethodDemo();
        double result; // call all versions of print() 
        ob.print(); //takes no parameter 
        ob.print(10); //takes one int type parameter 
        ob.print(10, 20); //takes two parameter 
        result = ob.print(123.25); //takes one parameter 
        System.out.println("Result of ob.print(123.25): " + result);
    }
}

The output of the program is:

No parameters 
a: 10 
a and b: 10 20 
double a: 123.25 
Result of ob.test(123.25): 15190.5625

 

Overloading Constructors

Same as method overloading, constructors can be overloaded. All the rules are same as that of method overloading mentioned above.

We've presented one example of constructor overloading here.

public class ConstructorOverloading {
    //default constructor
    public ConstructorOverloading(){
        System.out.println("Object of this class ConstructorOverloading is created .");
    }
    //constructor with one parameter
    public ConstructorOverloading(int argumentNum){
        System.out.println("The argument passed at object creation is "+argumentNum);
    }
    //constructor with different parameter
    public ConstructorOverloading(int num1, int num2){
        System.out.println("The sum of the numbers is: "+(num1+num2));
    }

    public ConstructorOverloading(double v) {
        System.out.println("The double value passed: "+v);
    }
}

public class ConstructorExample {
    public static void main(String[] args) {
        ConstructorOverloading newObj0=new ConstructorOverloading();
        ConstructorOverloading newObj01=new ConstructorOverloading(4);
        ConstructorOverloading newObj1=new ConstructorOverloading(5.1);
        ConstructorOverloading newObj2=new ConstructorOverloading(2,3);
    }
}

The ouput of the program is:

Object of this class ConstructorOverloading is created .
The argument passed at object creation is 4 
The double value passed: 5.1
The sum of the numbers is: 5
Constructors and Garbage Collection
Java String Class