2

I have two interfaces:

interface A {
    public void pull(SomeclassA a, SomeclassB b); 
}

interface B {
    public void make(SomeClassM m, SomeclassN n); 
}

In each interface I have a method with same number of parameters. Then I have a class with methods using both the above two interfaces, that is:

public class C {

    public void test(B b) {

    }

    public void test(A a) {

    }

}

I need to use the class with Java 8 lambda expression. How will Java recognize which interface is passed in to a method of this class?

public static void main(String[] args) {
    C c = new C(); 
    c.test(
        (a , b ) ->{

        }
    ); 
}
5
  • one of the methods gets selected implicitly, for the other one you should either change to inner class creation or try a cast to the proper interface. But you sould definitely change the method names so that they are unique. Commented Oct 19, 2016 at 11:11
  • The call is ambiguous: Ideone Commented Oct 19, 2016 at 11:12
  • @TimothyTruckle: is java 8 lambda expression created without awareness of overloading methods in java.... what if I created API that have overloading method? Commented Oct 19, 2016 at 11:25
  • 1
    looks like. but on the other hand you should not overload methods when you plan to use labda expressions. This significanly harms the readability of your code. Commented Oct 19, 2016 at 11:33
  • The intended solution is to redesign C. Overloading lambda accepting methods is a bad idea most of the time. Commented Oct 19, 2016 at 18:25

1 Answer 1

4

You would get a compilation error, since the compiler has no way of choosing between the two overloaded test methods.

To fix that you'll have to specify the types of the lambda arguments.

For example :

public static void main(String[] args) {
    C c = new C(); 
    c.test(
        (SomeClassM a , SomeClassN b) -> {

        }
    ); 
}

will cause test(B b) to be chosen.

Sign up to request clarification or add additional context in comments.

5 Comments

There is no compilation error if I created a class with overloading methods (same name and number parameter, but different in type). I think that is written in java 8 specification...
@MohammadFajar I tried your code and got a compilation error (I did use different class names instead of SomeClassX). The method test(B) is ambiguous for the type C.
Or it may be c.test((A)(a, b) ->{ });
@Mohammad Fajar: nobody said that you get a compilation error when compiling the class C with the overloads. You’ll get a compilation error at the place where you try to make the ambiguous call with an untyped lambda expression. But note that when you compile C using javac with -Xlint option (strongly recommended), you’ll get a warning: [overloads] test(B) in C is potentially ambiguous with test(A) in C
Explicitly typed lambda is good. As a general rule of thumb avoiding overloading is always better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.