What are the possible ways to add methods in interface which is already's been used by 100's of classes?

default method in interface
interface in java
functional interface
java 8 interface
java interface static method
built-in functional interfaces in java
what is the difference between a method reference and a lambda expression?
static block in interface java 8

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.

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

Interview Question, If that is not possible, for example because the code in stake is outside of you are actually allowed to change, and what "minimal way" actually means. As this interface has already been implemented in many classes is a  You could add an extended interface, derived from the original one, with the new method added, and make the code which uses that interface in combination with the new method expecting to get an object of type "ExtendedInterface".

Since you have had a java-8-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.

Java Interfaces, Java interfaces specify what methods a class implementing that This Java interface tutorial explains how Java interfaces work, and how to use them. However, it is possible to provide default implememntations of a method in a Java If the API just adds the method to the desired interface, all classes  I am not aware of way to implement methods within all the classes that implement an interface but if the implementation of the logic in the method is the same, I would recommend that you create a base class and inherit from that new class so you don't have to re-implement the same logic over and over.

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.

Java 8 Interface Changes – default method and static method, This way we can add the default methods to existing interfaces without bothering added default method"); } /* Already existing public and abstract method * We  For example, you can write a private method in the interface and the code will still compile and work. A body for a method or indexer, property, or event accessor. Private, protected, internal

Static and Default Methods in Interfaces in Java, Some of them have been already covered in this article. Default interface methods are an efficient way to deal with this issue. They allow us to add new methods to an interface that are automatically available in the Furthermore, it's even possible to make the Car class use both sets of default methods: ? That means all the methods in an interface are declared with an empty body and are public and all fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword.

Java 8: Default and Static Methods Guide, Static Methods. In addition to adding instance methods to an interface, we are also able to add static methods. The syntax is similar… we just use the “static” modifier in the method signature and include a method body. All method declarations in an interface, including default methods, are implicitly public, so you can omit the public modifier. With this interface, you do not have to modify the class SimpleTimeClient , and this class (and any class that implements the interface TimeClient ), will have the method getZonedDateTime already defined.

Interfaces in Java, Interfaces specify what a class must do and not how. It is the blueprint of the class​. We can now add default implementation for interface methods. This default  An interface can extend another interface, in a similar way as a class can extend another class. Extending Interfaces. An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

Comments
  • Depends what you need it to do. You could add a default method.
  • How to do if my program is running on lower than 8?
  • @AjayChauhan You can not do that below Java 8. You would need to create a new interface.
  • @AjayChauhan default and static methods in interfaces are new since Java 8. If you use a version prior to Java 8, you'll have to implement the methods to each of your 100+ classes I'm afraid.
  • There is also the option to use static methods if we are already using Java 8.
  • @MuratKaragöz Edited it to the answer, including a link of the differences between default and static methods in interfaces.