Skip to content

What Are The 3 Types Of Interfaces Available 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

A) 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);
}

B) 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.

import java.util.Arrays;
import java.util.List;

public record Book(
    String title,
    String author,
    Integer year,
    Integer copiesSold,
    Double rating,
    Double costInEuros) {
  // statements
}
@FunctionalInterface
public interface BookService {
  void addBook(Book book);
}

C) 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 Streams APIJava

Gopi Gorantala Twitter

Gopi is a software engineer with over 14 years of experience. He specializes in Java-based technology stack and has worked for various startups, the European government, and technology giants.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.