Wednesday 27 October 2021

How method overloading works with null values in Java?

 

What is method overloading?

Methods within a class can have the same name if they have different parameter lists.

 

For Example

   int sum(int a, int b);

   int sum(int a, int b, int c);

 

Both methods have the same name sum, and parameters are different. Overloading methods is differentiated by the number and type of arguments passed to the method. The return type alone is not sufficient to differentiate two overload methods.

 

OverloadEx.java

package com.sample.app.overloading;

import java.util.Arrays;

class OverloadEx {

	void print(Object obj) {
		System.out.println("I am object " + obj);
	}

	void print(String s) {
		System.out.println("I am String " + s);
	}

	public static void main(String args[]) {
		OverloadEx obj = new OverloadEx();
		
		obj.print("Hi!!!");
		obj.print(Arrays.asList(2, 3, 5, 7));
	}
}

 

Output

I am String Hi!!!
I am object [2, 3, 5, 7]

obj.print("Hi!!!");

In the above example, we are passing a string argument, Java choose the most specific method from the overloaded methods (In this case Java choose ‘void print(String s)’).

 

obj.print(Arrays.asList(2, 3, 5, 7));

In this case, I am passing an array list as argument to the print method, Java choose the method ‘void print(Object obj)’.

 

Let’s see how Java behaves with null values.

Let’s pass a null as an argument to the print method. In this case, null can be assigned to either String or to Object. In this case, Java has two choices available to choose, so Java will have to find the most specific one.

 

Since Object is the super-type of String type, the string version is more specific than the Object version. So, if only those two methods exist, the String version will be chosen.

 

Let’s confirm with below example.

 

OverloadEx.java

package com.sample.app.overloading;

class OverloadEx {

	void print(Object obj) {
		System.out.println("I am object " + obj);
	}

	void print(String s) {
		System.out.println("I am String " + s);
	}

	public static void main(String args[]) {
		OverloadEx obj = new OverloadEx();

		obj.print(null);
	}
}


Output

I am String null


Let’s add one more overloaded version of ‘print’ (void print(Integer i)

) method and see.

 

Now I have below three methods.

void print(Object obj)

void print(String s)

void print(Integer i)

 

Now, when you call the print method with null argument, Both String, Integer are specific than Object version, but none is more specific than the other, so Java can't decide which one to call.

 

OverloadEx.java

package com.sample.app.overloading;

class OverloadEx {

	void print(Object obj) {
		System.out.println("I am object " + obj);
	}

	void print(String s) {
		System.out.println("I am String " + s);
	}
	
	void print(Integer i) {
		System.out.println("I am Integer " + i);
	}

	public static void main(String args[]) {
		OverloadEx obj = new OverloadEx();

		obj.print(null);
	}
}


When you compile above program, you will get below error.

$ javac OverloadEx.java 
OverloadEx.java:18: error: reference to print is ambiguous
		obj.print(null);
		   ^
  both method print(String) in OverloadEx and method print(Integer) in OverloadEx match
1 error


How to resolve above problem?

Explicitly mention which one you want to call by casting the argument to the appropriate type.

 

Example

obj.print((Integer)null);

 

Find the below working application.


 

OverloadEx.java

package com.sample.app.overloading;

class OverloadEx {

	void print(Object obj) {
		System.out.println("I am object " + obj);
	}

	void print(String s) {
		System.out.println("I am String " + s);
	}
	
	void print(Integer i) {
		System.out.println("I am Integer " + i);
	}

	public static void main(String args[]) {
		OverloadEx obj = new OverloadEx();

		obj.print((Integer)null);
	}
}


Output

I am Integer null

 

Reference

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

 

 

 

You may like

Interview Questions

Remove oldest entry from the map

Synthetic constructs in Java

What is bridge method in Java?

How HashSet behave while adding a duplicate value?

How to call a method asynchronously in Java?

No comments:

Post a Comment