NBKRIST – Java Hub

Interfaces and Lambda Expressions in Java

Expert View

As a software engineer, I have seen how Java 8 reshaped the language’s future. The introduction of lambda expressions and functional interfaces marked a major paradigm shift toward functional programming. Before this, interfaces only defined behavior — now, they empower modern software design. These enhancements allowed Java to compete with functional languages like Scala and Kotlin. Without lambdas and default methods, Java’s relevance in the industry could have faded. At NBKRIST, we train students to think functionally, making their code expressive, modular, and scalable.

1. Basic Interface Implementation and Polymorphism

Demonstrates how a class implements interfaces and polymorphism restricts references to interface-defined behaviors.

interface Alpha { void foo(); void boo(); } class AlphaImpl implements Alpha { public void foo(){ System.out.println("AlphaImpl foo"); } public void boo(){ System.out.println("AlphaImpl boo"); } }

2. Interface Inheritance and Polymorphism

One interface can extend another, forming a hierarchy of contracts.

interface Vehicle { void start(); void stop(); } interface Car extends Vehicle { void drive(); } class Sedan implements Car { public void start(){ System.out.println("Sedan engine started."); } public void stop(){ System.out.println("Sedan engine stopped."); } public void drive(){ System.out.println("Sedan is driving."); } }

3. Multiple Inheritance via Interfaces

Java achieves multiple inheritance through interfaces. This allows modular, decoupled design.

interface Music { void play(); } interface GPS { void track(); } class SmartWatch implements Music, GPS { public void play(){ System.out.println("Playing music..."); } public void track(){ System.out.println("Tracking via GPS..."); } }

4. Java 8 and 9 Interface Enhancements

Java 8 introduced default and static methods. Java 9 added private interface methods for reuse.

interface Transaction { void deposit(double amount); default void printStatement(){ System.out.println("Printing statement..."); } static void bankInfo(){ System.out.println("NBKRIST Bank - Functional Interface Example"); } } class Account implements Transaction { public void deposit(double amt){ System.out.println("Deposited: "+amt); } }

5. Lambda Expressions and Functional Interfaces

Functional interfaces have a single abstract method and can be implemented using lambda expressions.

@FunctionalInterface interface Adder { int add(int a,int b); } class LambdaTest { public static void main(String[] args){ Adder sum=(a,b)->a+b; System.out.println("Sum: "+sum.add(10,20)); } }

6. Lambda Expressions with Streams

Lambdas with Stream API provide concise data operations.

import java.util.*; class StreamDemo { public static void main(String[] args){ List nums=Arrays.asList(1,2,3,4,5,6,7,8,9,10); int result=nums.stream() .filter(n->n%2!=0) .mapToInt(n->n*n) .sum(); System.out.println("Sum of odd squares: "+result); } }

Practice Questions

1. What is a functional interface?
An interface with exactly one abstract method.
2. What are lambda expressions?
Concise syntax for implementing functional interfaces.
3. How does Java achieve multiple inheritance?
Through implementation of multiple interfaces.
4. What enhancements were added in Java 9 for interfaces?
Private interface methods for code reuse.
5. What is the benefit of lambdas with streams?
They allow cleaner, functional-style data processing.

Key Summary