×

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

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 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.