Java 8 Flashcards
Interface rules since Java 8 :
*Interface methods can have either “static” or “default” modifier
*Interface methods can have a body if they are “static” or “default” (not abstract)
Why default interface methods are added on java ?
If we add a new method in interface , we will be forced to implement it on all implementations, which is not the case for default interface methods which already define their methods body.
+ to support lambda expressions
Some default interface methods rules :
We can override a default interface method
What is Diamond Problem ?
Java doesn’t allow multiple inheritance for classes because compiler will be confused to chose which superclass method to use.
This happens also for interfaces , when a class implement multiple interfaces with the same default method.
How to resolve Diamond Problem for interface ?
We must provide implementation for common default methods of interfaces , where class chose which one to call.
What is static interface method ?
static interface method is similar to default method except that we can’t override it.
What is Functional Interface ?
*Functional interface is an interface where only a single abstract method is defined.
______________________________________________
=>Functional interface is implemented by Lambda expression.
=>Functional interface can have static and default methods
=>All methods must be public
=>Methods defined in class Object are not taken into account as being abstract methods
The use of @FunctionalInterface is optional ?
Yes , it is optional
What are the two advantages of @FunctionalInterface annotation ?
*indicates to compiler that it’s a functional interface , then compiler verify if all rules all respected
*javadoc will use annotation when generating documentation
What the package java.util.function provides ?
It provides standard functional interfaces.
What are the 4 standard functional interfaces provided by java 8 ? what is there definitions ?
*Consumer : defines a function that take an argument but returns no value
*Supplier : defines a function that return a value as the same type of argument
*Predicate : defines a function that expect different types of arguments and return a Boolean value.
*Function : defines a function that expect different types of arguments and return a value.
_______________________________________________
Other standard Functional interfaces :
*UnaryOperator : defines an operation that expect only one parameter and return a value
*BinaryOperator : defines an operation that expect two parameters and return a value
What is Lambda expression ?
Lambda expression is an anonymous method, that doesn t belong to any class and execucted on demand. It has no return type.
=> It is used to implement Functional interface and simplify functional method.
=>For parameters type,return type , exceptions : compiler deduce it from Functional Interface.
=> Lambda Expression can implement only one method from interface.
=> It is treated as a function , thus , there is no need to create .class file by compiler.
Which type of variables Lambda expression can capture ?
Local + instance + static variables
________________________________________
For local variable : they must be final of effictively final ( not declared final but don t change value ).
Method reference
Method reference is a simplified form of Lambda expression (both are similar). It allows as to invoke a pre-existant method : static method , non-static method or a constructor (instead of providing implementation like Lambda expression ).
=> It has three parts :
*A qualifier that specify the name of class or instance on which method will be invoked
*The operator ::
*An identifier that specify the name of method or new operator for constructor.
What is Stream ?
Stream is a sequence of elements that supports operations that can performed on these elements in a functional style.