Fundamentals 6 min read

Understanding Java Interfaces: Definition, Implementation, and Multiple Inheritance

This article explains Java interfaces as abstract contracts, demonstrates how to define an interface with method prototypes, shows concrete class implementations using the implements keyword, explores the benefits of separating contracts from classes, and illustrates implementing multiple interfaces in a single class with clear code examples.

Java Captain
Java Captain
Java Captain
Understanding Java Interfaces: Definition, Implementation, and Multiple Inheritance

Java interfaces act as abstract contracts that expose only method signatures, allowing classes to hide internal implementation details while providing a public API. The article uses the metaphor of a cup to illustrate how an interface defines capabilities such as addWater and drinkWater without specifying how they work.

Example interface definition:

interface Cup {
    void addWater(int w);
    void drinkWater(int w);
}

A concrete class implements the interface by providing method bodies and can maintain its own state:

class MusicCup implements Cup {
    public void addWater(int w) {
        this.water = this.water + w;
    }
    public void drinkWater(int w) {
        this.water = this.water - w;
    }
    private int water = 0;
}

Implementing an interface does not reduce the amount of code needed for a class, but it enforces a standard that improves quality and interoperability, similar to industry standards like USB.

Additional methods not declared in the interface can be added to the class, for example:

class MusicCup implements Cup {
    // ... previous methods ...
    public int waterContent() {
        return this.water;
    }
    private int water = 0;
}

Interfaces also enable multiple inheritance of type. By defining another interface, such as a music player, a class can implement both contracts:

interface MusicPlayer {
    void play();
}

class MusicCup implements MusicPlayer, Cup {
    // implement addWater, drinkWater, play, and private state
    public void addWater(int w) { this.water += w; }
    public void drinkWater(int w) { this.water -= w; }
    public void play() { System.out.println("la...la...la"); }
    private int water = 0;
}

The article concludes that interfaces provide a clear separation between what a class can do and how it does it, facilitating code reuse, standardization, and the ability to compose objects with multiple capabilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaprogrammingOOPfundamentals
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.