Interface Keyword in Java

Interface Keyword in Java

March 25th, 2026
862
15:00 Minutes

An interface is an object in Java that shares common behaviours but whose implementation does not depend on the implementation of the other classes or objects with which it interacts. It is a reference type that allows the ability to share behaviours between different classes. Having worked with the Java concepts and fundamentals for over 5 years, I have come to understand exactly how interfaces function within the Java programming language, how to write code using the interface keyword and how this can be beneficial in creating flexible and maintainable systems that are loosely coupled.

This article will give you an overview of the interface keyword in Java and show how to implement it through examples to develop your understanding. Let's get started!

Learn interface Keywords in Java with simple explanation, syntax & examples. Understand abstraction, polymorphism & implementation easily to enhance your Java programming skills.

What is an Interface Keyword in Java?

An interface in Java is a blueprint of a class that contains abstract methods and constants. It defines what a class must do, but not how it does it. Classes that implement an interface must provide the implementation of all its abstract methods. The interface keyword is used to create such a contract.

For example:

interface Animal {
    void sound();
}

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

public class Test {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

interface keyword in java

Syntax of Interface in Java

interface InterfaceName {
    // abstract methods
    returnType methodName();

    // constants
    int VALUE = 10;
}

Why Interfaces are Used in Java

Interfaces help define a set of rules that classes must follow. They make programs more flexible, reusable and easier to maintain by separating what a class does from how it does it. Here are some of its usages:

1. Achieve Abstraction: Interfaces allow developers to define what a class should do without specifying how it should do it. This helps hide implementation details and focus on functionality.

2. Support Multiple Inheritance: Java does not support multiple inheritance with classes, but a class can implement multiple interfaces, which allows it to inherit behavior from multiple sources.

3. Promote Loose Coupling: Interfaces reduce dependency between classes. Code that depends on interfaces rather than concrete classes becomes more flexible and easier to modify.

4. Improve Code Maintainability: By separating the contract from the implementation, developers can change implementations without affecting other parts of the application.

5. Enable Polymorphism: Interfaces allow objects of different classes to be treated as the same type, which makes it easier to write generalized and reusable code.

6. Support Design Patterns and Frameworks: Many Java frameworks and design patterns like Dependency Injection, Strategy Pattern and DAO pattern rely heavily on interfaces to build scalable and extensible applications.

Read Also: Java Tutorial for Beginners

How to Implement an Interface

In Java, an interface is like a blueprint that tells a class what methods it must have, but it does not provide the actual code for those methods. When a class implements an interface, it agrees to write the code for all the methods declared in that interface. This helps in creating organized, reusable and flexible programs.

1. Create an Interface

First, you define an interface using the interface keyword. Inside it, you only declare methods.

Example:

interface Animal {
    void sound();
}

In this example, Animal is the interface and it declares a method sound().

2. Implement the Interface in a Class

Create a class that implements the interface using the implements keyword. The class must provide the actual code for the method.

Example:

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

The Dog class implements the Animal interface and defines the sound method.

3. Use the Implemented Class

At last, you create an object of the class and call the method.

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) {
        Dog d = new Dog();
        d.sound();
    }
}

implement an interface in java

Examples of Interface Keyword in Java

The following examples demonstrate different ways interfaces can be created and used in Java programs:

Example 1: Basic Interface

Basic interface contains abstract methods. A class implements the interface and provides the method body.

Example Code:

// Interface
interface Animal {
    void sound();   // abstract method
}

// Class implementing interface
class Dog implements Animal {

    public void sound() {
        System.out.println("Dog barks");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

basic interface in java

How it Works:

1. An interface Animal defines a method sound() without implementation.

2. class Dog implements Animal must provide the method body.

3. In main(), we create a Dog object and call sound().

Example 2: Interface with Default Method

Java 8 introduced default methods in interfaces. A default method already has an implementation, so implementing classes do not need to override it.

Example Code:

interface Vehicle {

    void start();   // abstract method

    default void fuelType() {
        System.out.println("Petrol or Diesel");
    }
}

class Car implements Vehicle {

    public void start() {
        System.out.println("Car starts with key");
    }
}

public class Main {
    public static void main(String[] args) {

        Car c = new Car();
        c.start();
        c.fuelType();   // default method
    }
}

interface with default method in java

How it Works:

1. start is abstract: must be implemented.

2. fuelType is a default method: already implemented.

3. Car only implements start().

4. fuelType() can be called directly.

Example 3: Interface with Static Method

Java 8 also introduced static methods in interfaces.

  • Static methods belong to the interface itself
  • They are called using the interface name
  • They cannot be overridden

Example Code:

interface MathOperation {

    static int add(int a, int b) {
        return a + b;
    }
}

public class Main {

    public static void main(String[] args) {

        int result = MathOperation.add(5, 3);
        System.out.println("Sum = " + result);
    }
}

interface with static method in java

How it Works:

1. add() is a static method inside the interface.

2. No class implementation is required.

3. The method is called:

MathOperation.add(5,3);

Read Also: Java Interview Questions and Answers

Best Practices for Using Interfaces in Java

When you start following the best practices, it will always make sure that your code remains clean and easy to understand. Following are some practices that you can follow when you are working with Java interfaces:

1. Keep Interfaces Focused (Interface Segregation)

Design interfaces with a single responsibility. Avoid creating large interfaces that force classes to implement methods they don’t need.

Example:

interface Printer {
    void print();
}

interface Scanner {
    void scan();
}

2. Use Interfaces to Define Behavior, Not Implementation

Your interface should always describe what a class can do and not how it does it.

Example:

interface Payment {
    void pay(double amount);
}

class CreditCardPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid using Credit Card: " + amount);
    }
}

3. Prefer Interfaces Over Abstract Classes When Possible

An abstract class does not allow multiple inheritance of type, but an interface does.

Example:

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() { System.out.println("Duck flies"); }
    public void swim() { System.out.println("Duck swims"); }
}

4. Use Default Methods Carefully

Default methods help maintain backward compatibility but should be used sparingly to avoid confusing interface design.

Example:

interface Vehicle {
    default void start() {
        System.out.println("Vehicle starting...");
    }
}

Wrapping Up

In this blog, I have explained how the interface keyword in Java helps a contract that classes must follow by allowing different classes to implement the same behavior in their own way. It supports important concepts like abstraction, multiple inheritance of type, polymorphism and loose coupling. By using interfaces, developers can design programs that are more flexible, reusable and easier to maintain, which makes it an important part of building well structured Java applications.

FAQs

1. When to use a class and an interface in Java?

You can use a class when you want to define objects with properties and full method implementations and an interface is used when you want to define a set of methods that multiple classes must implement.

2. Which is better interface or abstract class?

We cannot decide on which is best as it all depends on the situation. Interfaces are better for defining behavior for multiple classes, while abstract classes are useful when classes share common code.

3. How do you declare an interface?

An interface is only declared by the interface keyword.

Explore Our Trending Articles-

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled content writer with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.