Fundamentals 3 min read

How to Extend a Java Interface Without Breaking Existing Implementations

The article explains why adding a method directly to an existing Java interface can break all classes that already implement it and demonstrates the correct approach of creating a new interface that extends the original, preserving compatibility.

Lisa Notes
Lisa Notes
Lisa Notes
How to Extend a Java Interface Without Breaking Existing Implementations

This learning note (Day 62) reflects on the difficulty of persistence and highlights Java's "write once, run anywhere" advantage for distributed, object‑oriented development.

The original interface DaoInit is defined with two methods:

public interface DaoInit {
    // interface method
    void doSomething(int x, double y);
    // interface method
    int doSomethingElse(String z);
}

If a third method is added directly to DaoInit:

public interface DaoInit {
    void doSomething(int x, double y);
    int doSomethingElse(String z);
    // newly added method
    boolean didItWork(int x, double y, String z);
}

all existing classes that previously implemented the old interface will fail to compile because they do not provide an implementation for didItWork. The author points out that these classes effectively no longer satisfy the interface contract.

The recommended solution is to keep the original interface unchanged and create a new interface that extends it, adding the extra method only to the new interface:

public interface DoItPlus extends DaoInit {
    // declare the additional method in the new interface
    boolean didItWork(int x, double y, String z);
}

Classes that need the new functionality can implement DoItPlus, while existing implementations continue to work with the unchanged DaoInit. This approach preserves backward compatibility and follows good OOP design principles.

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.

javaprogrammingsoftware designrefactoringoopinterfaceinheritance
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

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.