100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Interfaces in Java

Learn how Java interfaces define a contract of behavior, support multiple inheritance of type, and how default/static methods work since Java 8.

OOP AdvancedIntermediate9 min readJul 7, 2026
Analogies

1. Introduction

An interface in Java is a reference type, similar to a class, that can contain only abstract methods (before Java 8), constants, default methods, static methods, and private methods (Java 9+). It defines a contract of *what* a class must do, without dictating *how* it does it.

🏏

Cricket analogy: A team's playing contract with the ICC states a bowler must deliver six-ball overs and follow the no-ball rule, but never dictates whether he bowls seam or spin, just like an interface.

Interfaces are Java's mechanism for achieving multiple inheritance of type, since a class can implement any number of interfaces even though it can extend only one class.

🏏

Cricket analogy: An all-rounder like Ravindra Jadeja can simultaneously fulfill the bowler and batsman roles on the team sheet, even though he belongs to only one franchise, mirroring a class implementing many interfaces but extending one class.

2. Syntax

java
interface InterfaceName {
    // constant field (implicitly public static final)
    int MAX_VALUE = 100;

    // abstract method (implicitly public abstract)
    void doSomething();

    // default method (Java 8+)
    default void greet() {
        System.out.println("Hello from default method");
    }

    // static method (Java 8+)
    static void info() {
        System.out.println("Static interface method");
    }
}

class MyClass implements InterfaceName {
    public void doSomething() {
        System.out.println("Doing something");
    }
}

3. Explanation

Every method declared in an interface without a body is implicitly public and abstract — you never need to (and should not) write those keywords explicitly. Every field declared in an interface is implicitly public static final, meaning it behaves like a compile-time constant and must be initialized at declaration.

🏏

Cricket analogy: Just as every player automatically wears the team's public jersey number without needing to announce it, every interface method is silently public and abstract, and every field is silently a fixed constant like the boundary rope distance.

A class uses the implements keyword to signal that it fulfills an interface's contract, and it must override every abstract method of that interface (unless the class itself is abstract). A class can implement multiple interfaces separated by commas: class MyClass implements InterfaceA, InterfaceB. Similarly, one interface can extend multiple other interfaces, unlike classes which can extend only one superclass.

🏏

Cricket analogy: A player signing with Chennai Super Kings implements the franchise contract by fulfilling every clause, and one player can sign with both their national board and IPL franchise at once, unlike a club that plays in only one league.

Java 8 introduced default methods (with a body, usable without overriding) and static methods (called on the interface itself) to let interfaces evolve without breaking existing implementations. Java 9 added private interface methods to share code between default methods.

🏏

Cricket analogy: Java 8's default methods are like the DRS rule added mid-career that older umpires can use without retraining, and static methods are like ICC's own ball-tampering ruling that applies to the sport itself, not any one team.

Exam tip: All interface fields are implicitly public static final, and all abstract methods are implicitly public abstract. Writing these modifiers explicitly is legal but redundant — examiners often test whether you know they are optional, not required.

If a class implements two interfaces that both declare a default method with the same signature, the class MUST override that method itself to resolve the ambiguity, otherwise it is a compile-time error.

4. Example

java
interface Swimmer {
    void swim();

    default void breathe() {
        System.out.println("Breathing air");
    }
}

interface Runner {
    void run();
}

// A class can implement multiple interfaces
class Duck implements Swimmer, Runner {
    public void swim() {
        System.out.println("Duck is swimming");
    }

    public void run() {
        System.out.println("Duck is running");
    }
}

public class Main {
    public static void main(String[] args) {
        Duck duck = new Duck();
        duck.swim();
        duck.run();
        duck.breathe();
    }
}

5. Output

text
Duck is swimming
Duck is running
Breathing air

6. Key Takeaways

  • Interface methods without a body are implicitly public abstract.
  • Interface fields are implicitly public static final (constants).
  • A class can implement multiple interfaces, achieving multiple inheritance of type.
  • An interface can extend multiple other interfaces.
  • Java 8+ allows default and static methods with a body inside interfaces.
  • Conflicting default methods from two interfaces must be overridden explicitly.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#InterfacesInJava#Interfaces#Syntax#Explanation#Example#StudyNotes#SkillVeris