Ambiguty and no suitable method found error in java overloading

Viewed 1677

Suppose I have one overloaded class like below

class Test{
    public void m1(int a,float b){
        System.out.println("hello");
    }
    public void m1(float a,int b){
        System.out.println("hai"); 
    }
    public static void main(String[] args){
        Test t = new Test();
        t.m1(10,10);//error
        t.m1(10.5f,10.6f);//error
    }
}

when I call m1() method with two int values like m1(10,10) the error is

 error: reference to m1 is ambiguous, both method m1(int,float) in Test and method m1(float,int) in Test match
t.m1(10,10);
 ^

and when I call m1() method with two float values like m1(10.5f,10.6f) error is

error: no suitable method found for m1(float,float)
t.m1(10.5f,10.6f);
 ^
method Test.m1(float,int) is not applicable
  (actual argument float cannot be converted to int by method invocation conversion)
method Test.m1(int,float) is not applicable
  (actual argument float cannot be converted to int by method invocation conversion)

Can anyone please explain the reason, why this program shows two different types of errors?

1 Answers
Related