Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can interfaces evolve with time?

Tags:

Interfaces are great from a flexibility standpoint. But in case, where an interface is used by a large number of clients. Adding new methods to the interface while keeping the old mehtods intact will break all clients' code as new methods won't be present in clients. As shown below:

public interface CustomInterface {      public void method1();  }  public class CustomImplementation implements CustomInterface {      @Override     public void method1() {         System.out.println("This is method1");     } } 

If at some point later in time, we add another method to this interface all clients' code will break.

public interface CustomInterface {      public void method1();      public void method2();  } 

To avoid this we have to explicitly implement new methods in all clients' code.

So I think of interfaces and this scenario as following:

  1. Interfaces once written are like carving in stone. They are rarely supposed, and expected to change. And if they do, they come with a huge cost(rewriting the whole code) which programmers should be ready for.
  2. In continuation with the point above, Is it possible to write interfaces that can stand the test of time?
  3. How such a scenario is handled in interfaces where you expect additional functionality in future? That is anticipating change in the contract by which all clients are binded.

EDIT: Default method is indeed a nice addition to Java Interfaces which a lot of people have mentioned in their answers. But my question was more in the context of code design. And how forcing method implementation on the client is an intrinsic character of an interface. But this contract between an interface and a client seems fragile as functionality will eventually evolve.

like image 856
Meena Chaudhary Avatar asked Dec 26 '15 09:12

Meena Chaudhary


People also ask

What is the real time usage of interfaces?

Real-time interfaces are used to support master data management (moving data into and out of the master data hubs real time) as well as the movement of transactional data updates between applications.

Do interfaces extend?

An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends.

Do interfaces have polymorphism?

Interfaces formalize polymorphism. Interfaces allow us to define polymorphism in a declarative way, unrelated to implementation. Two elements are polymorphic with respect to a set of behaviors if they realize the same interfaces.

What is real life example of interface?

The Interface is a medium to interact between user and system devices. For example, in our real life, if we consider the interface to be given as an example is the air condition, bike, ATM machine, etc.


1 Answers

One solution to this problem was introduced in Java 8 in the form of default methods in interfaces. It allowed to add new methods to existing Java SE interfaces without breaking existing code, since it supplied default implementation to all the new methods.

For example, the Iterable interface, which is widely used (it's a super interface of the Collection interface) was added two new default methods - default void forEach(Consumer<? super T> action) and default Spliterator<T> spliterator().

like image 176
Eran Avatar answered Nov 09 '22 14:11

Eran