Given the following code, can someone please explain why the assertion returns true? Despite having searched around countlessly, I haven't been able to get any appropriate answer for why this might be the case, and what the Java feature(s) cause this behaviour and what restrictions / requirements I would have in similarly creating an interface such as this.
interface X {
default int foo() {return 1;}
String bar();
}
public class Exercise{
public static void main(String[]arg){
X foo1=()->"hello";
assert (foo1.bar()).equals("hello");
}
}
Lambda expressions are introduced in Java 8, and they can represent the instance of a functional interface. To explain the above statement, we are defining a functional interface named "SquareRoot.". It has only one abstract method, "findSquareRoot.". @FunctionalInterface annotations are used to declare an interface as a functional interface.
A functional interface is an interface that has exactly one abstract method. We can then implement this interface's method, through a lambda expression: StringConcat lambdaConcat = (String a, String b) -> { return a + " " + b;}; With this implementation, the concat () method now has a body and can be used later on:
Functional interfaces, which are gathered in the java.util.function package, satisfy most developers' needs in providing target types for lambda expressions and method references. Each of these interfaces is general and abstract, making them easy to adapt to almost any lambda expression.
where lambda operator can be: Please note: Lambda expressions are just like functions and they accept parameters just like functions. Note that lambda expressions can only be used to implement functional interfaces.
A lambda expression is a concise way to create an instance of a functional interface, which is an interface with one abstract method. Here, X
is a functional interface, with abstract method bar()
. You could implement X with:
class XImpl implements X {
public String bar() { return "Foo"; }
}
X instance = new XImpl();
or an anonymous class
X anon = new X() {
public String bar() { return "foo"; }
};
or a lambda
X lambda = () -> "foo";
Each of these instantiates an implementation of X
. What may be confusing you is the concision of the lambda; because X
has only one abstract method, bar
, you don't have to say you are implementing bar
-- the compiler figures it out. Since bar()
takes no arguments and returns String
, the compiler ensures that the shape of the lambda is compatible with the shape of the sole abstract method.
Because an X
has a bar()
method,
you can call it on each of these instances:
String s1 = instance.bar();
assertEquals(s1, "hello");
String s2 = anon.bar();
assertEquals(s2, "hello");
String s3 = lambda.bar();
assertEquals(s3, "hello");
and you get the same result for each.
Since method foo()
has default implementation, you only need to specify bar()
- which you do with X foo1 = ()->"hello"
.
So, your foo1.bar()
returns hello.
UPD: My answer may be unclear, so I should note that 'foo' is not treated as an abstract method by compiler (that lets your interface X
satisfy the requirements of declaring functional interface). Brian explains it clearer and more detailed in his answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With