Java Functional Interface abstract method and Lambda Expression parameter and return types

This post demonstrates the type of arguments we can pass to and return from a Lambda expression based on the functional interface abstract method definition.

Relax. The types must be sub-types or exactly match to those defined in the abstract method both for the parameters and return type.

Using the Function Interface

Consider the following method.

It accepts a Person object and a Function functional interface that accepts and returns a Person object.

Person class and its sub-class

The Student class extends the Person class.

Now Polymorphism allows us to pass or/and return Student object instead of Person objects.

The codes output


When we assign a Lambda expression to a variable, we need to declare that variable using Generics.

This results to a compilation error.


The Full Codes

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert