what is Interface in Java

What is Interface in Java?

June 20th, 2026
7185
4:00 Minutes

Interface in Java is the core concept every developer must know about. It explains the design and structure of an application by defining the abstract type to specify the behavior of a class. Do you know how? This blog here explains everything around what is interface in Java, including its definition, properties, relation with class and more. Let's begin with the definition:

What is Interface in Java?

An interface in Java is a group of abstract methods used to achieve a unique abstraction in object oriented programming. This abstraction involves defining methods even without actual implementations. Think of it as a reference type that is similar to the class. In some instances, interfaces also contain static methods, default methods, constants and nested types. One crucial fact is that method bodies only exist for default and static methods.

Now you might be wondering how to write an interface. It is very similar to writing a class. The difference is that a class always defines the behaviors and attributes of an object, whereas interface defines the behaviors implemented by the class. Therefore, if the class implementing the interface is not abstract, one may have to define all the methods of the interface.

When to Use Interface in Java?

With the last statement, you might be confused about when to use an interface in Java. In this section, we will understand its perfect use. Consider the following points to decide between interface and class:

Use Interface When

  • You want to define the rules of behavior that all classes can implement.
  • You want to achieve abstraction and different inheritance.

Use Classes When

  • You want to represent a real-world entity with behaviors (methods) and attributes (fields).
  • You want to build objects that involve state and actions to perform.
  • You want to define templates for objects with unique properties and functionality.

Want to Become a Java Developer?

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Properties of Interface in Java

Understanding what is interface in Java also involves knowing its properties. It helps in making good use of the Java interface. Consider the following:

  • It is a mechanism that helps to achieve abstraction and different inheritance.
  • Variables in an interface are static, public and final by default.
  • Here, a class depends on behavior instead of implementation, showing its loose-coupling approach.
  • It is used to define methods that must be implemented by other classes.

Advantages of Using Interface in Java

Interfaces offer many advantages in Java programming, especially when building scalable and flexible applications. They help developers create a proper structure for classes and improve code maintainability. By using interfaces, developers can achieve abstraction, loose coupling and multiple inheritance in a clean and efficient way. Below are some major advantages of interfaces in Java:

  • Supports Abstraction: Interfaces help hide implementation details and only expose essential functionalities to users.
  • Enables Multiple Inheritance: A class can implement multiple interfaces at the same time, allowing better flexibility in design.
  • Promotes Loose Coupling: Interfaces reduce dependency between classes, making applications easier to manage and update.
  • Improves Code Reusability: Different classes can implement the same interface and reuse common method structures.
  • Supports Polymorphism: Interfaces allow objects to take multiple forms, improving flexibility in method handling.
  • Better for Large Applications: Interfaces help teams follow consistent coding standards while developing enterprise-level applications.

Rekation Between Class and Interface in Java

Still confused between interface and class or having some doubts? This table will help you to gain a good understanding.

Feature Class Interface
Purpose Blueprint for objects that define state and behavior. A behavior contract that defines what a class should do.
Implementation Provides concrete implementation for methods. Before Java 8: No method implementation. Java 8+: Default and static methods can have implementation.
State Can have instance variables. Cannot have instance variables.
Constructors Can have constructors. Cannot have constructors.
Inheritance Extends one class (single inheritance). Implements multiple interfaces (multiple inheritance of type)
Access Members can have various access modifiers. All methods are implicitly public and abstract (pre-Java 8). Fields are public static finals.
Keywords class, extends, implements interface, implements
Instantiation Can be instantiated (unless abstract). Cannot be instantiated directly.
Abstract Methods Can have both concrete and abstract methods. If it has an abstract method, it must be declared abstract. All methods are implicitly abstract (pre-Java 8).  Can have default, static, and private methods (Java 8+).

Example of Using Interface in Java

Let's undersand Java interface with the help of some real-world examples:

Example 1: Using Interface with an Animal Class

Let's take a real-world example of using an interface to understand how to use it. Here is an instance showing how to define a contract that a class can fulfill.

/* File name : MammalInt.java */
public class MammalInt implements Animal {

   public void eat() {
      System.out.println("Mammal eats");
   }

   public void travel() {
      System.out.println("Mammal travels");
   } 

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
} 

interface Animal {
   public void eat();
   public void travel();
}

Output:

Mammal eats
Mammal travels

Explanation:

Here, the Animal interface defines a contract: eat() and travel() methods must be implemented. The MammalInt class implements this contract, providing its unique logic for "Mammal eats" and "Mammal travels." It also adds its own noOfLegs() method. This demonstrates how interfaces enforce behavior while allowing diverse class implementations, ensuring consistency and promoting polymorphism.

Example 2: Using Interface with a Student Class

This example shows how an interface can define a common behavior that a class must implement.

interface Person {
    void introduce();
}

class Student implements Person {
    public void introduce() {
        System.out.println("I am a student");
    }

    public static void main(String args[]) {
        Student s = new Student();
        s.introduce();
    }
}

Output:

I am a student

Explanation:

Here, the Person interface contains the introduce() method. The Student class implements the interface and provides its own implementation. This shows how an interface defines behavior while the implementing class decides how that behavior works.

Example 3: Using Interface with a Shape Class

This example demonstrates how different classes can follow a common contract defined by an interface.

interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }

    public static void main(String args[]) {
        Circle c = new Circle();
        c.draw();
    }
}

Output:

Drawing Circle

Explanation:

The Shape interface declares the draw() method. The Circle class implements the interface and provides the actual implementation. This allows different shape classes to follow the same structure while performing different actions.

Multiple Inheritance in Java Using Interface

One of the biggest advantages of interfaces is that they allow multiple inheritance in Java, which is not possible with classes. You might already know that a class can extend only one parent class due to single inheritance limitations. But what if you need a class to inherit behavior from multiple sources? This is where interfaces shine.

A single class can implement multiple interfaces at the same time. This means your class can follow multiple contracts without any conflict. Let’s understand this with a simple example:

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck is flying");
    }
    
    public void swim() {
        System.out.println("Duck is swimming");
    }
    
    public static void main(String args[]) {
        Duck d = new Duck();
        d.fly();
        d.swim();
    }
}

Output:

Duck is flying
Duck is swimming

Explanation:

Here, the Duck class implements both Flyable and Swimmable interfaces. It provides implementations for both fly() and swim() methods. This shows how interfaces enable multiple inheritance of type, allowing a class to exhibit behaviors from different contracts. This is a clean and safe way to achieve code reusability and flexibility.

Robust Features of Interface in Java (Introduced Recently in JDK 8)

Since Java 8, interfaces are no longer just abstract contracts. Oracle introduced powerful new features that make interfaces more functional and practical. Let’s explore the key additions:

1. Default Methods

Now you can provide a default implementation for methods in an interface. This helps in evolving interfaces without breaking existing code.

interface Vehicle {
    void start();
    
    default void honk() {
        System.out.println("Beep beep!");
    }
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car started");
    }
    
    public static void main(String args[]) {
        Car c = new Car();
        c.start();
        c.honk();
    }
}

Output:

Car started
Beep beep!

2. Static Methods

Interfaces can now have static methods with implementation. These are utility methods related to the interface.

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

class Calculator {
    public static void main(String args[]) {
        System.out.println("Sum: " + MathUtils.add(5, 3));
    }
}

Output:

Sum: 8

3. Private Methods (Introduced in Java 9+)

You can now define private methods inside interfaces to reuse code between default methods.

interface Logger {
    default void logInfo(String msg) {
        log(msg, "INFO");
    }
    
    default void logError(String msg) {
        log(msg, "ERROR");
    }
    
    private void log(String msg, String level) {
        System.out.println("[" + level + "] " + msg);
    }
}

These features make interfaces more powerful and reduce boilerplate code. They allow backward compatibility, code sharing, and better design patterns like the Strategy or Template Method.

Want to Learn Everything About Java Programming?

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Wrapping Up

This article has explained why, where and how to use interfaces in Java. It will help you to build a new skill to become a Java developer. To gain more knowledge and build better skills, refer to our Java tutorial for beginners. It will help you to understand all the essential concepts of this programming language.

Related Articles:

FAQs on What is an Interface in Java

Q1. Why use interfaces in Java?

Interfaces are used for many purposes, including abstraction, multiple inheritance, polymorphism and loose coupling.

Q2. What are interface rules?

These rules define a contract for classes to follow. They establish a set of methods that any class implementing the interface must provide. This ensures consistent behavior across different classes and promotes abstraction and loose coupling.

Q3. How do I name Java interfaces?

You should ensure that names should be capitalized like class names, following the UpperCamelCase convention. For example, Runnable, Serializable, List.

Q4. How does an abstract class differ from an interface in Java?

An abstract class can have both abstract and non-abstract methods, while an interface defines only method declarations and is used to achieve multiple inheritance.

Q5. How is an interface different from a class?

A class can contain method implementations. An interface only declares methods without implementations.

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.