Interface in Java

Last Updated : 3 Jan 2026

An interface in Java is used to define a common set of methods that a class must implement. An interface helps in achieving abstraction and supports multiple inheritance. In this chapter, we will learn how to define an interface, how to achieve abstraction and multiple inheritance using interfaces.

What is Interface in Java?

An interface is a blueprint of a class that contains static constants and abstract methods. Interfaces are used to achieve abstraction. An interface contains only abstract methods (methods without a body) and variables. It cannot be instantiated, similar to an abstract class, and represents an IS-A relationship.

Since Java 8, interfaces can include default and static methods. Since Java 9, interfaces can also contain private methods.

Why Use an Interface in Java?

An interface is used in Java for the following reasons:

  • To achieve abstraction by defining method signatures without implementation.
  • To support multiple inheritance, as a class can implement multiple interfaces.
  • To achieve loose coupling, making the code more flexible and easier to maintain.

Declaring an Interface

An interface is declared using the interface keyword. It provides complete abstraction, which means all methods are abstract by default, and all fields are public, static, and final. A class that implements an interface must provide implementations for all the methods declared in the interface.

Syntax of Interface Declaration

Here is the syntax to declare an interface:

Example of Declaring an Interface

Here is an example to declare an interface:

In this example, the Animal interface declares two methods: eat() and sleep(). Any class that implements the Animal interface must provide concrete implementations for these methods.

Implicit Modifiers Added by the Java Compiler

In a Java interface, the compiler automatically adds certain modifiers to its members. All methods are implicitly declared as public and abstract, even if these keywords are not explicitly written. Similarly, all fields declared in an interface are implicitly public, static, and final.

In other words, interface methods are public and abstract by default, while interface fields are public, static, and final by default.

Since Java 8, interfaces can include default and static methods, which are discussed later in this chapter.

Relationship Between Classes and Interfaces

As shown in the following figure, a class extends another class, an interface extends another interface, and a class implements an interface. This relationship defines how classes and interfaces interact and support inheritance and abstraction in Java.

The relationship between class and interface

Interface Examples

Practice the following examples to understand how interfaces are declared, implemented, and used in Java.

Example 1: Printable Interface

The following example shows an interface with a single method. The implementation of the interface is provided by a class.

Example

Compile and Run

Output:

Hello

Example 2: Drawable Interface

The following example demonstrates how multiple classes can implement the same interface. In a real scenario, the interface is usually defined by one party, while different classes provide their own implementations. The implementation details remain hidden from the user.

Example

Compile and Run

Output:

drawing circle

Example 3: Bank Interface

This example shows how an interface can be used to provide a common structure for related classes. Each bank class provides its own implementation of the interface method.

Example

Compile and Run

Output:

SBI ROI: 9.15
PNB ROI: 9.7
HDFC ROI: 8.7

Implementing Multiple Inheritance Using Interfaces

When a class implements multiple interfaces, or an interface extends more than one interface, it is known as multiple inheritance.

In other words, multiple inheritance is implemented by allowing a class to implement multiple interfaces or an interface to extend multiple interfaces.

 multiple inheritance in java

Example

The following example demonstrates how a class can implement multiple interfaces to achieve multiple inheritance.

Output:

printing data...
showing data...

Inheritance of Interfaces

A class implements an interface, whereas an interface can extend another interface. When one interface extends another, it inherits all the abstract methods of the parent interface.

A class that implements the child interface must provide implementations for all methods declared in both the parent and child interfaces.

Example

The following example demonstrates how one interface can inherit another interface and how a class implements the child interface by providing implementations for all inherited methods.

Output:

Hello
Welcome

Interfaces and Polymorphism

One of the main benefits of interfaces is that they support polymorphism. An interface reference can point to any object of a class that implements it. This allows the program to decide at runtime which method implementation to use.

Example

The following example demonstrates how an interface reference can point to an object of a class that implements it, showing polymorphic behavior.

Output:

Dog is eating
Dog is sleeping

In this example, myAnimal is an interface reference variable of type Animal, referring to a Dog object. It demonstrates polymorphic behavior, where the method calls are resolved at runtime based on the actual object type.

Java 8 Default Method in Interface

Since Java 8, we can have method body in interface. But we need to make it default method. Let's see an example:

Example

Compile and Run

Output:

drawing rectangle
default method

Java 8 Static Method in Interface

Since Java 8, we can have static methods in the interface. Let's see an example:

Example

Compile and Run

Output:

drawing rectangle
27

Nested Interface

An interface can be declared inside another interface. Such interfaces are called nested interfaces. A class can implement the outer interface, the nested interface, or both, depending on the requirement.

Example

The following example demonstrates how to declare and implement a nested interface:

Output:

Printing document...
Printing message...

Interface Vs. Abstract Class

An abstract class can have both abstract and concrete methods, constructors, and different types of variables, but it does not support multiple inheritance. An interface can only declare abstract methods (with default and static methods allowed since Java 8), has no constructors, and supports multiple inheritance.

Read more about the differences between abstract class and interfaces here: Java - Abstract Class Vs. Interfaces