Consider a Java class which represents a two-dimensional point.
The class Point2D is mutable: its state can be changed after construction, by invoking either of the setter methods (setX() or setY()).
An immutable interface for Point2D could be defined as:
By making Point2D implement ImmutablePoint2D, client code could now reference a type which does not have mutating methods, and thus appears immutable. This is demonstrated in the following example:
By referencing only the immutable interface, it is not valid to call a method which mutates the state of the concrete object.
An alternative to the immutable interface pattern is the immutable wrapper pattern.
Persistent data structures are effectively immutable while allowing modified views of themselves.
Immutable Interface, c2.com- immutable, mindprod.com- Peter Haggar, "Practical Java Praxis 65: Use Inheritance or Delegation to Define Immutable Classes", informIT, January 22, 2001 http://c2.com/ppr/wiki/JavaIdioms/ImmutableInterface.html ↩