What Are The 3 Types Of Interfaces Available in Java?
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
- It’s a common way to achieve abstraction in Java.
- The interface supports multiple inheritances.
- Loose coupling.
- 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.
- Normal Interfaces
- Single Abstract Interface (or) Functional Interfaces
- 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.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.