0% found this document useful (0 votes)
17 views3 pages

OOPS Lab 6

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

OOPS Lab 6

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab - 6

WAP to implement the following new features in Java.


1. Functional Interface
2. Lambda Expression: Write a Java program to implement a lambda expression to check if a
given string is empty.
3. Method References:
4. Default and Static Method in Interface
5. Inner Class

import java.util.function.Consumer;

@FunctionalInterface
interface MyFunctionalInterface {
void display(String message);
}

interface MyInterface {
void abstractMethod();

default void defaultMethod() {


System.out.println("This is a default method in the
interface.");
}

static void staticMethod() {


System.out.println("This is a static method in the
interface.");
}
}

public class CombinedExample implements MyInterface {


@Override
public void abstractMethod() {
System.out.println("This is the implementation of the
abstract method.");
}

public static void main(String[] args) {


// Lambda expression to check if a string is empty
MyFunctionalInterface checkString = (message) -> {
if (message.isEmpty()) {
System.out.println("The string is empty.");
} else {
System.out.println("The string is not empty: " +
message);
}
};

checkString.display("");
checkString.display("Hello, World!");

// Static method reference


Consumer<String> staticMethodRef =
CombinedExample::printMessage;
staticMethodRef.accept("Hello from static method
reference!");

// Instance method reference


CombinedExample example = new CombinedExample();
Consumer<String> instanceMethodRef =
example::printInstanceMessage;
instanceMethodRef.accept("Hello from instance method
reference!");

// Interface example
example.abstractMethod();
example.defaultMethod();
MyInterface.staticMethod();

// Inner class example


OuterClass outer = new OuterClass();
outer.createInnerClass();
}

public static void printMessage(String message) {


System.out.println(message);
}

public void printInstanceMessage(String message) {


System.out.println(message);
}
}

class OuterClass {
private String outerField = "Outer field";

class InnerClass {
public void display() {
System.out.println("Accessing outer field: " +
outerField);
}
}

public void createInnerClass() {


InnerClass inner = new InnerClass();
inner.display();
}
}

You might also like