Home Java Java Interface

Java Interface

Beginner ⏱ 9 min read Updated: Jul 2026

A Java Interface is one of the most important features of Object-Oriented Programming. It defines a contract that classes must follow. An interface contains method declarations that implementing classes must provide. It helps achieve abstraction, loose coupling, and supports multiple inheritance in Java.

Interfaces are widely used in enterprise applications because they improve code flexibility, make applications easier to maintain, and allow different classes to share common behavior without sharing implementation.

What is an Interface in Java?

An interface is a reference type declared using the interface keyword. It contains method declarations, constants, default methods, static methods, and private helper methods (Java 9 and later). A class uses the implements keyword to implement an interface.

💡
Key Point: An interface specifies what a class should do, while the implementing class decides how it should do it.

Why Use Interfaces?

Abstraction

Hides implementation details from users.

Loose Coupling

Makes applications flexible and easier to maintain.

Multiple Inheritance

A class can implement multiple interfaces.

Reusability

Common functionality can be shared across applications.

Syntax of Java Interface

Syntax
interface Animal{
    void sound();
}
class Dog implements Animal{
    public void sound(){
      System.out.println("Dog barks");
    }
}

Java Interface Example

Example
interface Animal{
    void sound();
}
class Dog implements Animal{
    public void sound(){
        System.out.println("Dog barks");
    }
  }
public class Main{
    public static void main(String args[]){
        Animal obj = new Dog();
        obj.sound();
    }
 }
Output
Dog barks

In this example, the Animal interface declares the sound() method. The Dog class implements the interface and provides the method body. When the method is called, the Dog class executes its own implementation.

Implementing Multiple Interfaces

Java does not allow multiple inheritance using classes, but it supports multiple inheritance through interfaces. A single class can implement two or more interfaces at the same time.

Example
interface Printable{
    void print();
}
interface Showable{
    void show();
}
class Demo implements Printable, Showable{
    public void print(){
        System.out.println("Printing...");
    }
    public void show(){
        System.out.println("Showing...");
    }
    public static void main(String args[]){
        Demo obj = new Demo();
        obj.print();
        obj.show();
    }
}
Output
Printing...
Showing...

Default Methods in Interface

Since Java 8, interfaces can contain default methods. These methods already have an implementation, so classes implementing the interface can use them directly without overriding them. However, they can still provide their own implementation if needed.

Example
interface Message{
    default void display(){
        System.out.println("Welcome to Java");
    }
  }
class Demo implements Message{
    public static void main(String args[]){
        Demo obj = new Demo();
        obj.display();
    }
}
Output
Welcome to Java

Static Methods in Interface

Java 8 also introduced static methods inside interfaces. These methods belong to the interface itself and are called using the interface name instead of an object.

Example
interface Demo{
    static void show(){
        System.out.println("Static Method");
    }
}
public class Main{
    public static void main(String args[]){
        Demo.show();
    }
}
Output
Static Method

Real-Life Example of Interface

Consider a payment application that supports multiple payment methods such as credit cards, debit cards, UPI, and net banking. All payment options perform the same operation—processing a payment—but each follows a different internal implementation. An interface named Payment can define a pay() method, while each payment class implements it according to its own logic. This approach makes it easy to add new payment methods without changing existing code.

Advantages of Java Interface

Multiple Inheritance

A class can implement multiple interfaces.

Better Design

Encourages loose coupling between classes.

Reusability

Common contracts can be reused across projects.

Easy Maintenance

Changes in implementation do not affect interface users.

Flexibility

Different classes can provide different implementations.

Interface vs Abstract Class

Interface Abstract Class
Declared using the interface keyword. Declared using the abstract keyword.
Supports multiple inheritance. Supports single inheritance.
No constructors. Can contain constructors.
Fields are public, static, and final by default. Can contain instance variables.
Defines a contract. Provides partial implementation.

Best Practices

Recommended Practices:
  • Use interfaces to define common behavior.
  • Keep interfaces focused on a single responsibility.
  • Avoid adding unnecessary methods.
  • Use default methods only when appropriate.
  • Prefer interfaces when multiple inheritance is required.

Summary

A Java Interface defines a contract that implementing classes must follow. It is one of the key OOP concepts used to achieve abstraction and loose coupling. Interfaces make applications flexible, reusable, and easier to maintain. They also enable multiple inheritance in Java and are widely used in enterprise applications and frameworks.