0

I tried different combination of Method Overloading with Autoboxing and Widening. I got stuck in here.

public void bite(int a, byte b){
  System.out.println("here in int-byte");
}

public void bite(byte a, int b){
  System.out.println("here in byte-int");
}

In main:

byte a=9,b=8;
//int a=9,b=8;
bite(a, b);

Here error

The method bite(int, byte) is ambiguous for the type Overloading

When I reverse the method definition occurrence like:

public void bite(byte a, int b){
  System.out.println("here in byte-int");
}

public void bite(int a, byte b){
  System.out.println("here in int-byte");
}

Then error:

The method bite(byte, int) is ambiguous for the type Overloading

Please explain how this is happening.

bot13
  • 99
  • 1
  • 15
  • Does this answer your question? [overloading method priority in java](https://stackoverflow.com/questions/22590914/overloading-method-priority-in-java) – P3arl Dec 31 '19 at 11:26
  • i already went through that widening, autoboxing concepts but I think here question is bit different , I am asking about how method been defined first is reporting error. – bot13 Dec 31 '19 at 11:30

1 Answers1

0

The Solution might be both are ambiguous as bite is calling with byte parameters only. So compiler doesn't know which method to be called as widening takes priority. Both methods have int data type, so compiler not able to discern which one to be widened.

bot13
  • 99
  • 1
  • 15