Bogdan Gusiev's blog

How to make good software for people


What do you expect from the interface?
05 May 2009

Read a great article today - Liskov Substitution Principle (LSP). It let me understand that Interface is not just a list of methods.

Let's review the following utility function:
public static void processList(List list) { list.clear(); list.add(new Object()); }
It accepts the object that implements java.utils.List interface as the parameter. What do you expect to receive in result? The list with one element, right?
But does the result really meet your expectation? Who knows, the object that implements list interface do not guarantee you 'Act as container' behavior. It just implements all the methods of the interface and nothing else.
Actually all methods of the interface should have have a notation of their usage. If you do not follow these notations - you should not inherit your object from the interface even if the object has all the methods of the interface.

Idea: Maybe one day programming languages will have unit tests as part of interface and validate each instance that implements this interface with them.

java programming interface idea lsp