Difference between normal interface and functional interface in Java
Difference between normal interface and functional interface in Java
I'm happy to help!
In Java, interfaces can be categorized into two types: normal interfaces and functional interfaces.
Normal Interfaces
A normal interface is a type of interface that provides a blueprint for classes to implement its methods without providing any implementation. In other words, it specifies the contract that any class implementing this interface must fulfill, but does not provide the actual implementation.
Here are some key characteristics of normal interfaces:
They do not contain method implementations. Any class can implement multiple interfaces. Interfaces cannot extend another interface or class (since Java 8). Interfaces cannot have state variables. Methods in an interface are implicitly abstract and public.For example:
public interface Printable {
void print();
}
Any class implementing the Printable
interface must provide its own implementation of the print()
method.
Functional Interfaces
A functional interface is a special type of interface that has a single abstract method (SAM) and is used as a target for lambda expressions or method references. This type of interface was introduced in Java 8 to simplify working with functional programming concepts, such as using lambda expressions to create small, one-off functions.
Here are some key characteristics of functional interfaces:
A functional interface must have exactly one abstract method (SAM). The SAM method must be declared without a return type or void. Functional interfaces can only extend thejava.lang.Object
class or another functional interface. Methods in a functional interface can override methods from its superclass. You can create lambda expressions or method references that implement functional interfaces.
For example:
@FunctionalInterface
public interface PrintFunction {
void print(String text);
}
You can create a lambda expression or method reference to implement this functional interface, like so:
PrintFunction printFunction = (text) -> System.out.println(text);
printFunction.print("Hello, World!");
In summary, normal interfaces provide a contract for classes to implement without providing implementation, while functional interfaces are special interfaces designed specifically for lambda expressions or method references. Understanding the difference between these two types of interfaces is crucial in Java programming.
Can we inherit functional interface in java with example
I'd be happy to help you with that.
In Java, a functional interface is an interface that has a single abstract method (SAM). This concept was introduced in Java 8 as part of the lambda expression feature. A functional interface can be used as a target for lambda expressions.
Here's an example:
Let's say we want to create an interface Runnable
that represents something that can run or execute. In this case, it would make sense for such an interface to have only one abstract method - the run()
method.
public interface Runnable {
void run();
}
Now, let's create a lambda expression that implements this interface:
Runnable runnable = () -> System.out.println("Hello, world!");
Here, the lambda expression () -> System.out.println("Hello, world!");
is equivalent to implementing the run()
method. We can then execute this lambda like any other Runnable:
runnable.run(); // Output: Hello, world!
The Runnable
interface in Java is a classic example of a functional interface.
Another example would be the Predicate
interface from the Java Collections Framework. This interface defines a single method - test()
. This can be used to filter out elements based on some condition:
public interface Predicate {
boolean test(T t);
}
Here's how you could use it:
List list = Arrays.asList("Hello", "world!", "lambda");
Predicate predicate = (s) -> s.length() > 5;
list.removeIf(predicate::test); // Remove all strings longer than 5 characters.
In the above example, we create a Predicate
that tests if a string is longer than 5 characters. We then use this predicate to filter out elements in the list.
These are just a few examples of how functional interfaces can be used in Java. They provide a powerful way to encapsulate complex behavior and make code more expressive and concise.