Adding new method in Java Interface so it involves minimal changes to inheriting classes

13,065

Solution 1

I would create an extension of your interface for only the classes that need the additional methods...

public interface BaseInterface {
    public int exampleMethod();
}

public interface ExtendedInterface extends BaseInterface {
    public int anotherMethod();
}

The thousands of classes already implement BaseInterface. For the classes that need the extra method, you change them to implement ExtendedInterface.

If your objects are stored in a collection such as a BaseInterface[] array, this still works because objects of type ExtendedInterface are also objects of type BaseInterface, so they can still be stored in the same common collection.

For example, this is still perfectly valid...

BaseInterface[] objects = new BaseInterface[2];
objects[0] = new ClassThatImplementsBaseInterface();
objects[1] = new ClassThatImplementsExtendedInterface();

However, if you need to access the new method of the ExtendedInterface, but the object is stored in a BaseInterface collection, you'll need to cast it into an ExtendedInterface before you can use it...

BaseInterface[] objects = new BaseInterface[1];
objects[0] = new ClassThatImplementsExtendedInterface();

if (objects[0] instanceof ExtendedInterface){
    // it is an ExtendedInterface, so we can call the method after we cast it
    ((ExtendedInterface)objects[0]).anotherMethod();
}
else {
    // it is a BaseInterface, and not an ExtendedInterface
}

This may or may not be suitable, depending on your usage.

If you really need all your thousands of objects to implement the new method, you'll have to add the method to the BaseInterface and then use a feature of your IDE or text editor to implement the method in all your classes. For example, you could open them all in a text editor and do a find-replace to find something thats common to each class, and replace it with the common code + the default code for the new method. Pretty quick and painless. I'm sure that some IDEs would probably also automatically add the method declaration to all inheriting classes, or at least have an option to do this in a right-click menu.

Solution 2

If the new method is a true extension of the interface, then the right thing to do is edit the interface and use your development environment's tools to find all the places where the new functionality must be implemented. Then do the work. Eclipse and Netbeans will do a fine job.

[NB I'm a bit surprised that refactoring tools don't take care of some of the manual effort, but so it is.]

If the new method won't be called most of the time in old code, consider the new interface to be an extension of the old one:

public interface NewInterface extends OldInterface {
    void newMethod();
}

If you have needs to pass old interface objects to new interface consumers with a null version of newMethod(), you can do something like:

public class NewInterfaceWrapper<T extends OldInterface> implements NewInterface {

    private T wrapped;

    public NewInterfaceWrapper(T wrapped) {
        this.wrapped = wrapped;
    }

    // Define all the old interface methods and delegate to wrapped.method 

    // Now provide the null implementation of new method.
    void newMethod() { }
}

...

wantsNewInterface(new NewInterfaceWrapper(oldImplementer));

It's not pretty, but big systems usually grow rough edges like this as they age.

Solution 3

There is no easy way to do that. If you add a method to the interface all implementing classes must override it. If you change the interface to an abstract class then you have to refactor the implementing classes as well.

But you have a class hierarchy right? So you can minimize the work by implementing that method in the base classes only. But that depends on your specific requirements and details so I guess happy implementing!

And if there's no easy class hierarchy that you can use to implement new methods like that perhaps it's time you think about a major rewrite in favor of future maintenance effort reduction.

Share:
13,065
Sougata Bhattacharya
Author by

Sougata Bhattacharya

Updated on June 03, 2022

Comments

  • Sougata Bhattacharya
    Sougata Bhattacharya about 2 years

    Possible Duplicate:
    Adding Extra method to interface

    There is scenario where I have Interface X, which has been implemented with my thousands of classes. Now I want to add new method in that Interface X. So how to make the changes in minimal way to solve the problem of overridden of methods in all my classes

  • Luiggi Mendoza
    Luiggi Mendoza about 12 years
    Have you read the link that @verisimilitude has posted in a comment? If you don't, then please read it, it will change your answer and your point of view.
  • Thihara
    Thihara about 12 years
    Yes but won't that lead to maintenance problems sooner or later? Also it depends on the type of method that's being introduced, if the method in question is something that will be used everywhere the interface if used as the type then this solution won't help much...
  • Luiggi Mendoza
    Luiggi Mendoza about 12 years
    It really depends, if you have a bunch, i.e. 10+, of classes and you just want that some of them, i.e. 2, then it will be like 2 classes implementing the new method and 8+ having it without an implementation (pretty bad design too). If all the classes implementing the actual interface should have this new method, then OP question will be out of place and I don't think OP needs that :).
  • Thihara
    Thihara about 12 years
    Yes my point is that solution may work for some cases and is bad advice for some cases. So the proper way isn't that in my opinion if its gonna involve long term maintenance as most projects do.. But thats just me.. To each their own! ;-)
  • Gene
    Gene about 12 years
    It looks like WATTO Studios and I have the same idea. Happy to delete if this is not useful.
  • wattostudios
    wattostudios about 12 years
    Keep your post - you've got a different approach with the wrappers - I hadn't thought of that!
  • paulsm4
    paulsm4 about 12 years
    This is the same thing verisimilitude already said.
  • wattostudios
    wattostudios about 12 years
    Agreed, but this is more specific for the requirement of the user, has example coding in it, and in my opinion is much easier to understand than the linked post. Plus, I just happen to agree with the approach, so why not write an answer to that effect.