-3

Suppose I have an interface

public interface Animal{
  public void operationWalk();
}

This animal interface is implemented by many classes, now how do I add method in this interface so it should not break the functionality of classes which are implementing it.

Naman
  • 27,789
  • 26
  • 218
  • 353
Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37

3 Answers3

4

You can use default method in Java 8 Read about it here

2

Since you have had a -tag, you have access to the default keyword for an interface method, which is new since Java 8.

So you could modify it to something along the lines of:

public interface Animal{
  public void operationWalk();

  default void doSomething(){
    // Your code here
  }
}

This default method can be added to the interface, without having to add the implementations to your child classes that use this interface.

Note that default methods can still be overwritten in the child classes if you choose to. If you never want to overwrite this method, you could use a static method in the interface instead. Here a bit more info about default vs static methods in interfaces.


If you are using a version prior to Java 8, I'm afraid the only options you have are:

1) If you need this new method in each of the 100+ classes that implement this interface, you'll have to add this new method to your interface and implement the new method in each of those classes.
2) If you need this new method in only a few of the 100+ classes, you can create a new interface and implement this new interface only in those classes, including the implementation of the method.
3) If you need a standard method that is the same for each child class, as your question suggests, you can create an abstract class implementing the Animal-interface with the implementation of the new method, and then change each of the 100+ classes to extend this abstract class. You'd still have to modify each of those classes, but then you do have a default implementation for your new method in Java 7. Note that all classes can only extend a single abstract class, so if any of your classes implementing the Animal-interface are already extensions of other classes, this option isn't possible.

Although I would personally check if you could update to Java 8 (or higher) instead, since it has the default keyword for interfaces that you need for the best solution in this case. Since Java 11 is already released, it's not a bad idea to update to a newer Java version anyway.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
0

Just to add a few options to what other people have already mentioned:

If all of the classes need to actually implement the new method, there's nothing for it but to implement them. You don't necessarily actually have to implement all of them at once, depending on how critical it is that all of them have it, but you'll presumably have to eventually do it.

If not all of your classes need to implement the new method, a second possibility - and I like this better - is to have a second interface that extends your "main" interface. That way only the subset of classes that need it can implement it. This could be inconvenient for Factory classes and whatnot, though.

A third possibility is to have some classes have "empty" implementations:

public void operationWalk() {
}

On the plus side, at least this is just copy-paste, so it won't take all that long.

A fourth possibility (and I really, really don't like this) is to have your methods throw exceptions in cases where the operation simply doesn't apply.

public void operationWalk() {
   // Throw some exception indicating that this operation doesn't apply here
}

This, of course, violates the Liskov Substitution Principle. It also often indicates a flawed object hierarchy in my opinion. I recommend not doing this unless you have to, as it can cause serious maintenance issues later if you're not careful, but it's at least a possibility.