Open In App

Method overloading and null error in Java

Last Updated : 18 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Java it is very common to overload methods. Below is an interesting Java program. 

Java




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();
 
        // This line causes error
        mv.fun(null);
    }
}


Output :

22: error: reference to fun is ambiguous
        mv.fun(null);
          ^
  both method fun(Integer) in Test and method fun(String) in Test match
1 error

The reason why we get compile time error in the above scenario is, here the method arguments Integer and String both are not primitive data types in Java. That means they accept null values. When we pass a null value to the method1 the compiler gets confused which method it has to select, as both are accepting the null. This compile time error wouldn’t happen unless we intentionally pass null value. For example see the below scenario which we follow generally while coding. 

Java




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);
    }
}


Output :

fun(Integer ) 

In the above scenario if the “arg” value is null due to the result of the expression, then the null value is passed to method1. Here we wouldn’t get compile time error because we are specifying that the argument is of type Integer, hence the compiler selects the method1(Integer i) and will execute the code inside that. Note: This problem wouldn’t persist when the overridden method arguments are primitive data type. Because the compiler will select the most suitable method and executes it.



Previous Article
Next Article

Similar Reads

How to do Method Overloading for Null Argument?
In Java, Method overloading is a process in which we can define a method with the same name but with a different parameter in the same class. This is a feature of OOP (object-oriented programming). It helps increase the readability of the code, as we can use the same name for a function performing the same task but with different parameters or diff
3 min read
Difference Between Method Overloading and Method Overriding in Java
The differences between Method Overloading and Method Overriding in Java are as follows: Method Overloading Method Overriding Method overloading is a compile-time polymorphism.Method overriding is a run-time polymorphism.Method overloading helps to increase the readability of the program.Method overriding is used to grant the specific implementatio
4 min read
Method Overloading and Ambiguity in Varargs in Java
Prerequisite - Varargs , Method Overloading Method Overloading in Varargs Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. We can overload a method that takes a variable-length argument by following ways: Case 1 - Methods wi
5 min read
Method Overloading with Autoboxing and Widening in Java
Let us go through the basic pre-requisites such as Method Overloading, Autoboxing, and Unboxing. So method overloading in java is based on the number and type of the parameters passed as an argument to the methods. We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler
7 min read
Method Overloading in Java
In Java, Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both. Method overloading in Java is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding. In Method overloading c
9 min read
Overloading Variable Arity Method in Java
Here we will be discussing the varargs / variable arity method and how we can overload this type of method. So let us first understand what a variable arity method is and its syntax. A variable arity method also called as varargs method, can take a number of variables of the specified type. Note: Until version 1.4 there is no varargs method. It was
5 min read
super keyword for Method Overloading in Java
We use Method overloading to use a similar method for more than one time. This can be achieved by using different parameters in the signature. In the below example a class GFG with three similar methods is available, though the three methods are overloaded they are provided with different parameters. The object of the class GFG calls a method with
4 min read
Java Program to Find Area of Rectangle Using Method Overloading
A rectangle is a simple flat figure in a plane. It has four sides and four right-angles. In a rectangle all four sides are not of equal length like a square, sides opposite to each other have equal length and both the diagonals of the rectangle have equal length. Method overloading allows different methods to have the same name, but different signa
2 min read
Java Program to Find Area of circle Using Method Overloading
A circle is a simple shape consisting of all the points in the plane that are equidistant from a point known as the center of the circle. In this article, we will learn how to find the area of the circle using the method overloading. Terminology: Method Overloading: Method overloading allows different methods to have the same name, but different si
3 min read
Java Program to Find Area of Square Using Method Overloading
A square is a simple flat shape in a plane, defined by four points at the four corners. It has four sides with equal length and four corners with right angles. Method Overloading: Method overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of in
3 min read
Article Tags :
Practice Tags :