Skip to content

Types Of Interfaces in Java

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

What is an Interface?

An interface is a collection of abstract methods and constant fields a class can implement. It defines a contract for classes that implement it, specifying what methods they should implement and what fields they should have.

An interface is a reference type, just like a class, which can be declared using the interface keyword.

An interface allows you to provide a different implementation at runtime, inject dependencies, separate concerns, and use different implementations for testing.

Benefits

  1. It’s a common way to achieve abstraction in Java.
  2. The interface supports multiple inheritances.
  3. Loose coupling.
  4. Interfaces are also used to achieve multiple inheritances.

Code

interface A {
  // members of A
  public void myFirstMethod();
}

interface B {
  // members of B
  public void mySecondMethod();
  public int myThirdMethod();
}

class C implements A, B {
    // abstract members of A
    // abstract members of B
}

What are the different types Of interfaces available in Java?

There are 3 types of interfaces available in Java 8.

  1. Normal Interfaces
  2. Single Abstract Interface (or) Functional Interfaces
  3. Marker Interfaces

1. Normal Interfaces

This is a general interface that we use in Java programming. We discussed these types of interfaces above. You can have any number of methods in it.

Normal interfaces are interfaces that have more than one abstract method. They define a set of related behaviors that a class can implement.

Examples of normal interfaces in Java include List, Set, and Map.

In the following example, we have BookService that contains multiple abstract methods.

public record Book(
    String title,
    String author,
    Integer year,
    Integer copiesSold,
    Double rating,
    Double costInEuros) {
}
import java.util.List;

public interface BookService {
  void addBook(Book book);

  void deleteBook(Long bookId);

  void updateBook(Long bookId, Book book);

  void deleteBooksByAuthor(String authorName);

  List<Book> getAllBooks();
  
  Book findBookById(Long bookId);
}

2. Functional Interface or Single Abstract Interfaces

Functional interfaces are interfaces that have exactly one abstract method. They represent a single unit of behavior that can be used as a lambda expression or method reference.

These interfaces are also called SAM interfaces. In this, only one abstract method is present. We can make an interface containing a single abstract method as a functional interface by annotating it with @FunctionalInterface.

Examples of functional interfaces in Java include Runnable, Comparator, and Supplier.

An example is shown below. You'll learn more about functional interfaces in the next chapter in detail.

class Book {
  String title;
  String author;
  Integer year;
  Integer copiesSoldInMillions;
  Double rating;
  Double costInEuros;

  public Book(String title, String author, Integer year, Integer copiesSoldInMillions, Double rating, Double costInEuros) {
    this.title = title;
    this.author = author;
    this.year = year;
    this.copiesSoldInMillions = copiesSoldInMillions;
    this.rating = rating;
    this.costInEuros = costInEuros;
  }

  public Double getCostInEuros() {
    return costInEuros;
  }

  @Override
  public String toString() {
    return "Book{" +
      "title='" + title + '\'' +
      ", author='" + author + '\'' +
      ", year=" + year +
      ", copiesSoldInMillions=" + copiesSoldInMillions +
      ", rating=" + rating +
      ", costInEuros=" + costInEuros +
      '}';
  }
}
@FunctionalInterface
public interface BookService {
  void addBook(Book book);
}

3. Marker Interface

An interface without any methods or fields is called a marker interface. They indicate that a class implements a certain concept or behavior.

Examples of marker interfaces in Java include Serializable, Cloneable, and Remote.

Serializable is a marker interface in Java, and this is how it is defined in Java API.

public interface BookService {
    // no methods in it.
}

In addition to these interfaces, Java also allows interfaces to have default and static methods, which can provide default implementations or utility methods that can be called without an interface instance.

Overall, interfaces are an important feature of Java that allows for abstraction, polymorphism, and flexibility in designing software systems.

They are a powerful tool for defining contracts between classes and can help make code more modular and maintainable.

In the next chapter, we learn about functional interfaces in detail.

Java

Gopi Gorantala Twitter

Gopi is a highly experienced Full Stack developer with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.

Comments


Related Posts

Members Public

How To Write Lambda Expressions

This lesson introduces the basic lambda expression structure with tips to write efficient code.

Members Public

What Are Lambda Expressions?

This is an introductory lesson on lambda expressions. You will learn about the lambda operator, expression, syntaxes and more!

Members Public

Power Of Two (Exercise Problem)

This is an exercise problem for your practice. Try to come up with an approach and solve it by yourself. Good Luck!