what is a functional interface in java code example
Example 1: interface function
interface SearchFunc {
(source: string, subString: string): boolean;
}
Example 2: why we use interface in java
interface Animal {
void child();
}
class Cat implements Animal {
public void child() {
System.out.println("kitten");
}
}
class Dog implements Animal {
public void child() {
System.out.println("puppy");
}
}
public class LooseCoupling{
public static void main(String args[]) {
Animal obj = new Cat();
obj.child();
}
}
Example 3: how to declare a interface in java
interface Appendable {
void append(string content);
}
class SimplePrinter implements Appendable {
public void append(string content) {
System.out.println(content);
}
}
class FileWriter implements Appendable {
public void append(string content) {
}
}
0
how to implement a interface in java
Example 4: creating the functional interface in java
package com.concretepage;
@FunctionalInterface
public interface Calculator {
long calculate(long num1, long num2);
}