Showing posts with label Evolving Interfaces. Show all posts
Showing posts with label Evolving Interfaces. Show all posts

Thursday, 14 January 2016

Java Tutorial : Java Interface (Evolving Interfaces using extends)

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

What Are Evolving Interfaces in Java?

In Java, once you publish an interface and classes start implementing it, changing that interface directly can cause problems. If you add a new abstract method to an existing interface—like adding sayBye() to a DoIt interface—every class that implements DoIt will break until it provides an implementation for that new method.

To evolve an interface without breaking existing classes, you can create a new interface that extends the original interface.

How to Evolve an Interface Using extends

Instead of modifying the original interface, follow this clean approach:

  • Keep the Original Interface Intact: Leave DoIt with its original sayHi() method so existing classes continue working without changes.
  • Create an Extended Interface: Define a new interface, such as DoItPlus, that uses extends DoIt and declares the new sayBye() method.
  • Upgrade When Ready: Existing classes can stay on DoIt, or they can implement DoItPlus later to support both sayHi() and sayBye().

Code Example: Evolving Interfaces in Java

public interface DoIt {
  void sayHi();
}

public interface DoItPlus extends DoIt {
  void sayBye();
}

public class MyClass implements DoItPlus {
  public void sayHi() { System.out.println("Hi"); }
  public void sayBye() { System.out.println("Bye"); }
}

Key Takeaways

  1. Backward Compatibility: Extending an interface protects existing implementations from compilation errors.
  2. Flexible Upgrades: Classes can adopt the new DoItPlus interface only when they are ready to implement the added methods.
  3. Clean Polymorphism: You can still reference a MyClass object using either a DoIt or DoItPlus reference.

Watch More Java Tutorials:

Java Tutorial : Java Interface (Evolving Interfaces)

Master Core Java Step-by-Step!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential interview insights!

Subscribe Now on YouTube

What are Evolving Interfaces in Java?

In software development, systems rarely stay static. Over time, libraries and frameworks must adapt to new requirements, meaning interfaces published in early versions eventually need new operations. This concept of updating an interface over time is known as interface evolution.

However, evolving an interface in Java has historically posed a massive challenge: how do you add new methods without breaking existing classes that have already implemented the original contract?

The Challenge of Evolving an Interface

Prior to Java 8, interfaces could only declare abstract methods. This caused significant maintenance friction:

  • Breaking Existing Implementations: Adding a single abstract method forced every existing implementing class to either implement it immediately or fail to compile.
  • Rigid Library Upgrades: Core platform libraries (such as the Java Collections Framework) could not easily add modern features like forEach() or lambda-supported methods without breaking third-party code.
  • Workaround Complexity: Developers had to create new child interfaces extending the original contract just to add functionality cleanly.

How Java Solved Interface Evolution

Java addressed interface evolution directly through modern language enhancements:

// Original Interface
interface NotificationService {
  void sendEmail(String message);

  // Evolved method added later without breaking existing implementations
  default void sendSMS(String message) {
    System.out.println("Default SMS sender: " + message);
  }
}

// Older class continues to compile without modification
class LegacyAlertSystem implements NotificationService {
  @Override
  public void sendEmail(String message) {
    System.out.println("Sending email: " + message);
  }
}

Because sendSMS() is declared as a default method, LegacyAlertSystem compiles and runs without having to write an explicit implementation.

Key Strategies for Evolving Interfaces

  1. Interface Extension (Pre-Java 8 Approach): Create a child interface (e.g., interface EvolvedService extends BaseService) to declare new methods while leaving the parent unchanged.
  2. Default Methods (Modern Approach): Supply a concrete fallback implementation using the default keyword so implementing classes inherit it automatically.
  3. Static Utility Methods: Add shared helper operations directly to the interface to provide companion utilities without altering instance contracts.

Watch More Java Interface Tutorials:

Tutorials