Monday, May 24, 2021

Java function overloading

This tiny bit is about function overloading.

function name along with it's parameters is part of function signature. Function overloading is a feature where you can declare multiple functions with same name but different number and type of parameters. Here differentiation by just return type is not possible.


public class DemoMainOverload {

    public static void main(String args[])
    {
        System.out.printf("%s \n",main());
        DemoMainOverload obj = new DemoMainOverload();
        System.out.println(obj.main(4));
    }

    public static String main()
    {
        return "Main method overloading";
    }

    public String main(int val)
    {
        return "Instance method main with parameter "+val;
    }
}

In this code snippet main method is overloaded two times. this code generates the below output.



Main method overloading 
Instance method main with parameter 4

No comments:

Post a Comment