-
Notifications
You must be signed in to change notification settings - Fork 48
Single Abstract Method (SAM)
This annotation can be used for the definition of an interface. Once the annotation is used to define the interface, the compiler will force to check whether the interface has one and only abstract method, otherwise an error will be reported. But this annotation is not necessary, as long as it conforms to the definition of a functional interface, then this interface is a functional interface.
/**
* When a method with SAM as a parameter is called, a Lambda expression can be used as this parameter.
* According to the principle of SAM, in this interface, only one method needs to be implemented,
* but there are exceptions as below:
* 1. default methods and static methods do not affect the contract of the
* functional interface, feel free to use them.
* 2. Object public methods can be overrided, such as equals, toString, hashcode, etc.
* clone method is protected, so it cannot be overrided.
*/
@FunctionalInterface
public interface IFITest {
void test(String s);
// The class that implements the interface and sub-interface will not
// inherit the static methods in the interface
// this is a static method
static void test1() {
System.out.println("test1");
}
// this is a default method
default void test2() {
System.out.println("test2");
}
@Override
String toString();
@Override
boolean equals(Object object);
@Override
int hashCode();
}
public class FITest {
public static void main(String[] args) {
String text = "Hello World";
printString(text, System.out::print);
}
private static void printString(String text, IFITest iFITest) {
iFITest.test(text);
}
}
/* Output */
Hello World
@FunctionalInterface
public interface IFITest2 {
// Must be the same as the abstract method of IFITest
void test(String s);
}
@FunctionalInterface
public interface IFITest3 extends IFITest, IFITest2 {
// Must be the same as the abstract method of IFITest and IFITest2
void test(String s);
}
public class FITest {
public static void main(String[] args) {
String text = "Hello World";
printString(text, System.out::print);
}
private static void printString(String text, IFITest3 iFITest3) {
iFITest3.test(text);
}
}
/* Output */
Hello World
Peer Learning
Codecrunch Contributions
Piazza Contributions
Wiki Contributions
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Sunfire
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
CS2030 Contents
Lecture 1 SummaryCompile-run vs Run-time Summary
Quick Guide To Abstraction
Generics and Variance of Types
Comparable vs Comparator
Summary of completable future
CS2030S Notes
ELI5 Optional.map vs Optional.flatMap
PECS Example Code
Java Collection Framework (Iterator)
Generic
Generic Type Parameter and Generic Wildcard
Calculator
Lambda-Expression
Single Abstract Method (SAM)
Method Reference
Functional Interfaces 2
Simple Usage of Sandbox
Associative-but-not-commutative
Higher Order function
Functional Programming
Calculator With Functor
Eager Evaluation VS Lazy Evaluation
Simple Usage of Lazy Evaluation
Lazy Evaluation for LazyList
Lazy Evaluation for BinaryTree
Stream
Parallel Stream
Optional
Simple Usage of Stream
Asynchronous Programming
Notes on CompletableFuture
Notes on CompletableFuture 2
Simple Usage of CompletableFuture
Mind Map
Exception Handling
Links
CS2030 Java Style Guide
CS2030 Javadoc Specification
JDK 11 Download Link
JDK 11 API Docs
Codecrunch
Piazza Forum