×

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple methods with the same name exist since the compiler cannot determine which method should be called. The ambiguity problem in Java is a problem that arises when Java code is written in such a way that it can be interpreted in more than one way. This can lead to unexpected results and can be difficult to debug. Examples of ambiguity problems include code that uses the same variable name for different purposes in different parts of the code, code that uses multiple nested if-statements to evaluate conditions, and code that uses multiple overloaded methods with different parameters. Other ambiguity problems can arise when a programmer uses language features such as generics, enums, and annotations.

The challenges the Java language definition does not explicitly describe are known as ambiguities. Our views are supported by the various outputs generated by various compilers on several example programs. Here, we'll talk about things in this order.

Method of ambiguity in method overloading

  • only use Varargs in your methods
  • methods that use Varargs and other parameters

Ambiguity in multiple inheritances

  • Diamond Problem

Type 1: Method overloading with ambiguity

When you overload a method, you risk putting the compiler in a scenario where it is unsure which method to employ. Consider the following declarations for the overloaded computeBalance() method:

File Name:AmbiguityInJava.java

// Main class declaration
public class AmbiguityInJava {
    // static function named fun with double variables as parameters
   static void fun(double ... Val){
      System.out.print("fun(double ...): " + "Number of args: " + Val.length );
	// using each loop to iterate over the parameterized variable 
      for(double a : Val)
      System.out.print(a + " ");
      System.out.println();
   }
   // static function named fun with boolean variables as parameters
   static void fun(boolean ... Val){
      System.out.print("fun(boolean ...) " + "The number of arguments: " + Val.length);
      for(boolean a : Val)
	// // printing the each value
      System.out.print(a + " ");
	// // printing the new line
      System.out.println();
   }
   // Main section of the program where execution begins
   public static void main(String s[]){
       // calling the function
      fun(11.56, 34.78);
	// printing the function call
      System. out.println("Calling fun() ");
      // Calling the function
      fun(true, false);
	// printing the function call
      System.out.println("Calling fun() ");
      // Calling the function
      fun();
	// printing the function call
      System.out.println("Calling fun() ");
   }
}

Output

AmbiguityInJava.java:19: error: reference to fun is ambiguous
      fun();
      ^
  both method fun(double...) in Main and method fun(boolean...) in Main match
1 error

Explanation

A function called "fun" that accepts a configurable number of floating point numbers is defined in the Demo class. Using a "for" loop, the values are printed on the console. The parameters of this function are boolean values with changing numbers, which is overloaded. Using a "for" loop, the output is displayed on the console.

The word "fun" is called three times in the main function: once with floating point values, once with Boolean values, and once without any parameters. The console shows the exception that it generates.

Ambiguity Errors

Using generics creates a new kind of error called ambiguity, which you must prevent. Ambiguity errors happen when two generic declarations that seem mutually exclusive resolve to the same erased type, resulting in a conflict. Here is an instance of method overloading in action.

With kind 1, as described above, we are currently good to go. Overloading of methods in varargs.

Due to overloading, various methods with the same name but distinct signatures are possible. The signatures can vary depending on the input parameters' quantity, nature, or both. The following strategies can be used to overload a method that accepts a variable-length argument:

Case 1: Only Varargs parameters are used in methods

Java uses the type difference in this situation to choose which overloaded method to call. If one method signature is objectively stricter than the other, Java selects the stricter one without making a mistake.

File Name: Ambiguity2.java

// class declaration
public class Ambiguity2 
{
	static void fun(float... a)
	{
		System.out.println("float varargs");
	}
	// function with integer parameters
	static void fun(int... a)
	{
		System.out.println("int varargs");
	}
	// function with double parameters
	static void fun(double... a)
	{


		System.out.println("double varargs");
	}


// Main section of the program from where execution begins
	public static void main( String[] s)
	{
		// Calling the method
		fun();
	}
}

Output

int varargs

Explanation

Int is more specific than double, which results in this outcome. According to JLS section 15.12.2.5, one member method must be chosen to supply the descriptor for the run-time method dispatch if more than one is available and appropriate to a method invocation. The most specific approach is selected based on the Java programming language type promotion. The following rules define the direct supertype relation among the primitive types in this situation:

The following order is used: following order is used:

double > float

float  > long

long   > int

int    > char

int    > short

short  > byte

Case 2

File Name: Ambiguities.java

class Ambiguities {
	static void fun(int... x)
	{
		System.out.print("fun(int ...): "+ "Number of args: " + x.length+ " Contents: ");
		for (int a: x)
			System.out.print(a + " ");
		System.out.println();
	}
	static void fun(boolean... y)
	{
		System.out.print("fun(boolean ...) "+ "Number of args: " + y.length	+ " Contents: ");
		for (boolean x : y)	
			System.out.print(y + " ");
		System.out.println();
	}
	public static void main(String s[])
	{
		fun(6, 4, 3);
		fun(true, true, false);
	}
}

Output

fun(int ...): Number of args: 3 Contents: 6 4 3 
fun(boolean ...) Number of args: 3 Contents: [Z@769c9116 [Z@769c9116 [Z@769c9116

Example 2

Program

File Name:AmbiguityProblem.java

class AmbiguityProblem{
	static void func(int... x)
	{
		System.out.print("fun(int ...): "+ "Number of args: " + x.length+ " Contents: ");


		for (int a : x)
			// Printing statement
			System.out.print(a + " ");


		// New line 
		System.out.println();
	}
	static void func(boolean... x)
	{


		System.out.print("fun(boolean ...) "+ "Number of args: " + x.length+ " Contents: ");
		for (boolean a : x)


			System.out.print(a + " ");
		System.out.println();
	}
	public static void main(String args[])
	{


		func(5, 7, 13);


		func(false, true, false);
		func();
	}
}

Output

Main.java:28: error: reference to func is ambiguous
		func();
		^
  both method func(int...) in Main and method func(boolean...) in Main match
1 error

Explanation

Explanation of output: This instance's fun () overloading the desired method is ideal. However, the final call to "fun()," which can also be interpreted in the code, prevents this program from compiling.

Type 2

 Complex inheritances with ambiguity. When two classes are related, they can inherit each other's properties. The extends keyword can be used to define this relationship as follows:

 public class A extends C{}

A superclass or parent class is a class from which properties are inherited, and a subclass or child class is a class from which those properties are inherited. The superclass members are copied in the sub-class object during inheritance. Therefore, you can access the members using the sub-class object.

Example

File Name: Ambiguity.java

// Java Program to illustrate Diamond Problem Ambiguity
public class abstract Demo {


	// Abstract method declaration
	public abstract demo();
}
public class S1 extends Demo {
	public void demo()
	{
		System.out.println("demo method of super1");
	}
}


// Class 3
// Helper class extending Class 1
public class S2 extends Demo {


	// Method of this base class
	public void demo()
	{
		// Print statement whenever this method is called
		System.out.println("demo method of super2");
	}
}
// Main class extending the classes s1 and s2
public class Ambiguityextends S1, S2 {
	// Main section of the program from where execution begins
	public static void main(String args[])
	{
		// Creating object for the class SubClass
		SubClass o1 = new SubClass();
		// calling the function using the object created
		o1.demo();
	}
}

Output

Compilation failed due to the following error(s).
Main.java:2: error:  expected
public class abstract Demo {
            ^
Main.java:2: error:  expected
public class abstract Demo {
                          ^
Main.java:5: error: illegal start of expression
	public abstract demo();
	^
Main.java:5: error: invalid method declaration; return type required
	public abstract demo();
	                ^
Main.java:31: error: '{' expected
public class Main extends S1, S2 {
                            ^
5 errors

Explanation

The basic inheritance rule states that both demo() methods should be copied into the subclass object, leaving the subclass with two methods that share the same prototype (name and arguments). If you use the object of the subclass to call the demo() function, the compiler will then be in a scenario where it is unclear which method to call. In Java, this problem is referred to as the diamond problem.


Related Topics

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.