Interfaces and Lambda Expressions
Interfaces and Lambda Expressions
EXPRESSIONS
INTERFACES AND LAMBDA
EXPRESSIONS
OBJECTIVES:
KNOW WHAT AN INTERFACES IS
UNDERSTAND THE VARIOUS TYPES OF
INTERFACES
KNOW WHAT A LAMBDA
EXPRESSION IS
UNDERSTAND THE VARIOUS TYPES OF
LAMBDA EXPRESSIONS
INTERFACES
An interface describes a set of methods that can
be called on an object, but does not provide
concrete implementations for all the methods.
You can declare classes that implement (i.e.,
provide concrete implementations for the
methods of) one or more interfaces.
Each interface method must be declared in all the
classes that explicitly implement the interface.
Once a class implements an interface, all objects
of that class have an is-a relationship with the
interface type, and all objects of the class are
guaranteed to provide the functionality described
by the interface. This is true of all subclasses of
that class as well.
INTERFACES
Usefulness of Interfaces:
Interfaces are particularly useful for
assigning common functionality to
possibly unrelated classes. This allows
objects of unrelated classes to be
processed polymorphically— objects of
classes that implement the same interface
can respond to all of the interface
method calls.
INTERFACES
Standardizing Interactions
Interfaces define and standardize the ways in
which things such as people and systems can
interact with one another.
Using an Interface
To use an interface, a concrete class must specify
that it implements the interface and must
declare each method in the interface with the
signature specified in the interface declaration.
USING INTERFACES CONT’D
To specify that a class implements an
interface add the implements keyword and
the name of the interface to the end of your
class declaration’s first line. A class that does
not implement all the methods of the
interface is an abstract class and must be
declared abstract.
NOTE:
Methods defined interfaces are implicitly
public and abstract.
Values are implicitly public, static, and final.
INTERFACES
Interfaces are implemented and not
extended. The code below shows how the
Payable interface is implemented by two
disparate classes: Employee and Invoice.
This implies that objects of both classes
are also indirect classes of Payable.
Note that any class that implements an
interface must declare the methods of the
interface else it must be declared as
abstract.
IMPLEMENTING INTERFACES
public class Employee implements Payable {
private double basicPay = 20500.25;
private int hrsWorked = 20;
Sequenceable e_toomuch;
start = 2;
e_toomuch = ( a, d, n ) -> {
int counter = a;
while( counter <= n ) {
System.out.printf( ―%d\n‖, counter );
counter += d;
} // end while
}; // end block lambda
More Examples – Block Lambda
public interface Accumulateable {
double akowonJo(double x[ ] );
} // end interface Accumulateable
---------------------------------------------------
double kudi[ ] = { 10.15, 5.25, 12.75, 7.15 };
Accumulateable addache;
addache = ( owo ) -> {
double s = 0;
for(double naira : owo )
s += naira;
return s;
};
More example continued
double totalKudi = addache.akowonjo( kudi );
JOptionPane.showMessageDialog( null,
String.format(―Total amount in naira: ―
+ ―%,.2f‖, totalKudi );
Lets consider yet another example:
// A functional interface that takes two int parameters
// and returns a boolean result.
interface NumericTest {
boolean test( int n, int m );
} // end functional interface
More example continued
NumericTest isFactor = (n, d) -> (n % d) == 0;
if(isFactor.test(10, 2))
System.out.println("2 is a factor of 10");
if(!isFactor.test(10, 3))
System.out.println("3 is not a factor of 10");
System.out.println();