Sam Stewart

Ramblings of a discombobulated programmer

3 notes &

An Interface is a Contract

In OOP, an interface is quite simply a contract. If we examine the definition of a traditional contract we can begin to see the similarities:

A written or spoken agreement, esp. one concerning employment, sales, or tenancy, that is intended to be enforceable by law.

In particular, notice the word agreement which is the essence of a contract. When you specify an interface in Java, Objective-C, C++, etc., you are guaranteeing anyone who uses the class a certain conduct.  This removes any uncertainty on the callers side and allows the calling class to make firm assumptions. The interface defines a point of intersection among two (possibly quite large) components. Both components behave differently, but they are connected by a thin interface or contract which guarantees a certain subset of known behavior.

Filed under oop design programming

  1. samthestewart posted this