ADSENSE HERE!
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
Idea: Maybe one day programming languages will have unit tests as part of interface and validate each instance that implements this interface with them.
ADSENSE HERE!
These are only dreams. They won't come true till very long
ReplyDeleteI don't see any reason why this dream can not be implemented tomorrow.
ReplyDeletenice idea. why not?
ReplyDeleteYes, nothing stops the implementor to fill the methods body with throw new RuntimeException(). There are languages (maybe Eiffel) that make preconditions, invariant and postconditions part of a method signature, however this seems too complicated for a general purpose language like Java. Moreover, today there's more interest in dynamic languages and duck typing that in strict interface correctness. :)
ReplyDeleteWell, you should start coding now! ;-)
ReplyDelete[...] 2009 About half of a year ago I was writing about object interface and Liskov Substitution Princeple. In short: Any class instance that extends the base class should pass all unit tests behaviour [...]
ReplyDelete