What do you expect from the interface?

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 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.
ADSENSE HERE!

6 comments:

  1. These are only dreams. They won't come true till very long

    ReplyDelete
  2. I don't see any reason why this dream can not be implemented tomorrow.

    ReplyDelete
  3. nice idea. why not?

    ReplyDelete
  4. Yes, 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. :)

    ReplyDelete
  5. Well, you should start coding now! ;-)

    ReplyDelete
  6. [...] 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

Komen dong, tapi yang sopan dan tidak spam ya

Copyright © Spesial Unik. All rights reserved. Template by CB. Theme Framework: Responsive Design