Javatpoint Logo
Javatpoint Logo

Types of Interfaces in Java

In Java interfaces are essential, when it comes to setting class contracts and guaranteeing code consistency in the realm of Java programming. It serves as a class's blueprint by outlining a number of methods that the implementing class is required to implement. The abstraction, modularity, and maintainability of their code can all be achieved by developers by employing interfaces. The various interface types in Java will be discussed in this section.

Marker Interfaces

Marker interfaces, often called tag interfaces, are interfaces without any methods declared. Simply said, they serve as markers, signalling that a class that implements the marker interface has particular properties or qualities. The Java runtime environment and other tools make use of these APIs to deliver extra functionality or data. Serializable, Cloneable, and Remote are some instances of marker interfaces in Java.

Functional Interfaces

As part of the improvements to lambda expressions and functional programming, functional interfaces were added to Java 8. An interface that has precisely one abstract method is said to be functional. Additionally, default and static methods are possible. Many Java functional programming capabilities, like lambda expressions and method references, make use of functional interfaces. Predicate, Consumer, Function, and Supplier are just a few of the built-in functional interfaces offered by the java.util.function package.

Single Abstract Method (SAM) Interfaces

Prior to Java 8, there were certain kinds of functional interfaces called SAM interfaces. These interfaces can have any number of default or static methods, but only one abstract method. Runnable, Comparator, and MouseListener are a few pre-Java 8 APIs that contain SAM interface examples. With the introduction of functional interfaces in Java 8, SAM interfaces can be used with lambda expressions and considered as functional interfaces.

Normal Interfaces

The most prevalent kind of interfaces in Java is normal interfaces. They have one or more abstract methods that the classes that implement the interface must implement. Beginning with Java 8, normal interfaces can additionally contain default and static methods. Normal interfaces are used by developers to specify the rules or requirements that classes must follow. List, Set, and Map, all of which are found in the java.util package, are examples of typical Java interfaces.

Multiple Inheritance Interfaces

Java permits classes to implement multiple interfaces but does not support multiple class inheritance. Interfaces that are implemented by a class to simultaneously define numerous contracts or inherit behaviour are known as multiple inheritance interfaces. A class can offer the functionality outlined by each interface by implementing several interfaces. With the help of this functionality, developers can reuse code and increase the adaptability of their ideas.

Here are examples of code for each type of interface in Java along with their corresponding output:

Marker Interfaces

MarkerInterfaceExample.java

Output:

true

Functional Interfaces

FunctionalInterfaceExample.java

Output:

false
true

Single Abstract Method (SAM) Interfaces:

SAMInterfaceExample.java

Output:

Performing action

Normal Interfaces

NormalInterfaceExample.java

Output:

Hello, World!

Multiple Inheritance Interfaces:

MultipleInheritanceInterfaceExample.java

Output:

Method 1
Method 2

These examples demonstrate the usage and behavior of different types of interfaces in Java. You can run each code snippet individually to observe the corresponding output.

In conclusion, Java interfaces offer a potent method for specifying contracts and attaining code modularity. Interfaces are essential to Java development, whether they are marker interfaces that indicate specific traits, functional interfaces that enable functional programming, regular interfaces that define contracts, or multiple inheritance interfaces that combine several contracts. Java developers are better equipped to build clear, extendable code when they are aware of the various interface types and their functions.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA