What is Single Abstract Method (SAM) and Functional Interface in Java

Carvia Tech | May 27, 2019 | 1 min read | 1 views | Java 8


Interface that has single abstract method (SAM), is known as functional interface. We can supply a lambda expression whenever an object of an functional interface is expected.

Conceptually, a functional interface has exactly one abstract method.

Example of functional interface:

Callable is a FunctionalInterface
package java.util.concurrent;

@FunctionalInterface    (1)
public interface Callable<V> {

    V call() throws Exception;
}
1 The compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

Aren’t all methods in Interface are abstract?

  1. default methods in interface can have an implementation, so they are not counted as abstract.

  2. Interface can redeclare methods from the Object class, such as toString() and clone() method, these declarations do not make the abstract method since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

For example, java.util.Comparator interface is a FunctionalInterface even though it overrides equals() method from Object class.

Comparator definition
@FunctionalInterface
public interface Comparator<T> {

    int compare(T o1, T o2);

    boolean equals(Object obj);
}

Top articles in this category:
  1. Top 50 Multi-threading Java Interview Questions for Investment Bank
  2. Citibank Java developer interview questions
  3. Goldman Sachs Java Interview Questions for Senior Developer
  4. RBS Java Programming Interview Questions
  5. Morgan Stanley Investment Banking Java Interview Questions
  6. Sapient Global Market Java Interview Questions and Coding Exercise
  7. BlackRock Top Java Interview Questions: Investment Banking Domain



Find more on this topic:
Java Interviews image
Java Interviews

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info