How to do method overloading for null argument?

In this Tutorial, we will learn How to do method overloading for null arguments? as we can by specifying the variable type which has stored the null value to understand follow the example given below. Although we can not pass the argument as null making a variable, store the null value in it.

Overloading

Method Overloading

Method overloading is using the same name of the function by simply changing the argument or signature or we can say we can do say in two ways first by changing the type of argument and second by changing the number of arguments.

And we can pass the null argument through a variable where we initialize the variable and store a null value to it and then we can pass it as an argument to call the specific function.

To understand it better follow the example and understand it in a better manner.

public class Test
{
    // Overloaded methods
    public void fun(Integer i)
    {
        System.out.println("fun(Integer ) ");
    }
    public void fun(String name)
    {
        System.out.println("fun(String ) ");
    }

    // Driver code
    public static void main(String [] args)
    {
        Test mv = new Test();
        
        Integer arg = null;

        // No compiler error
        mv.fun(arg);
    }
}

 

Here we can see how we have initialized a variable arg and stored a null value to it the type of variable arg is an integer so it is going to call the function which has arguments as integers Which is the first one so the output of the above example code will be fun(integer ) as the function corresponding to it is called.

To learn more How to do method overloading for null argument? at Null Arguments

and also visit Java Tutorials to learn more in java and solve more java related problems take people face during execution.

And also visit Python’s list: What is the difference Between methods Append and Extend? to learn more about python and other programing related problems.

Leave a Comment

%d bloggers like this: