6.031 — Software Construction
Spring 2017

Reading 14: Interfaces & Enumerations

Java Tutor exercises

Keep making progress on Java by completing this category in the Java Tutor:

Software in 6.031

Safe from bugsEasy to understandReady for change
Correct today and correct in the unknown future. Communicating clearly with future programmers, including future you. Designed to accommodate change without rewriting.


The topic of today’s class is interfaces: separating the interface of an abstract data type from its implementation, and using Java interface types to enforce that separation.

After today’s class, you should be able to define ADTs with interfaces, and write classes that implement interfaces.


Java’s interface is a useful language mechanism for expressing an abstract data type. An interface in Java is a list of method signatures, but no method bodies. A class implements an interface if it declares the interface in its implements clause, and provides method bodies for all of the interface’s methods. So one way to define an abstract data type in Java is as an interface, with its implementation as a class implementing that interface.

One advantage of this approach is that the interface specifies the contract for the client and nothing more. The interface is all a client programmer needs to read to understand the ADT. The client can’t create inadvertent dependencies on the ADT’s rep, because instance variables can’t be put in an interface at all. The implementation is kept well and truly separated, in a different class altogether.

Another advantage is that multiple different representations of the abstract data type can co-exist in the same program, as different classes implementing the interface. When an abstract data type is represented just as a single class, without an interface, it’s harder to have multiple representations. In the MyString example from Abstract Data Types, MyString was a single class. We explored two different representations for MyString, but we couldn’t have both representations for the ADT in the same program.

Java’s static type checking allows the compiler to catch many mistakes in implementing an ADT’s contract. For instance, it is a compile-time error to omit one of the required methods, or to give a method the wrong return type. Unfortunately, the compiler doesn’t check for us that the code adheres to the specs of those methods that are written in documentation comments.

For the details of how to define interfaces in Java, consult the Java Tutorials section on interfaces.

reading exercises

Java interfaces

Consider this Java interface and Java class, which are intended to implement an immutable set data type:

    /** Represents an immutable set of elements of type E. */
    public interface Set<E> {
        /** make an empty set */
A       public Set();
        /** @return true if this set contains e as a member */
        public boolean contains(E e);
        /** @return a set which is the union of this and that */
B       public ArraySet<E> union(Set<E> that);    

    /** Implementation of Set<E>. */
    public class ArraySet<E> implements Set<E> {
        /** make an empty set */
        public ArraySet() { ... }
        /** @return a set which is the union of this and that */
        public ArraySet<E> union(Set<E> that) { ... }
        /** add e to this set */
        public void add(E e) { ... }

The line labeled A is a problem because Java interfaces can’t have constructors.

(missing explanation)

The line labeled B is a problem because Set mentions ArraySet, but ArraySet also mentions Set, which is circular.

(missing explanation)

The line labeled B is a problem because it isn’t representation-independent.

(missing explanation)

ArraySet doesn’t correctly implement Set because it’s missing the contains() method.

(missing explanation)

ArraySet doesn’t correctly implement Set because it includes a method that Set doesn’t have.

(missing explanation)

ArraySet doesn’t correctly implement Set because ArraySet is mutable while Set is immutable.

(missing explanation)


Recall that a type is a set of values. The Java List type is defined by an interface. If we think about all possible List values, none of them are List objects: we cannot create instances of an interface. Instead, those values are all ArrayList objects, or LinkedList objects, or objects of another class that implements List. A subtype is simply a subset of the supertype: ArrayList and LinkedList are subtypes of List.

“B is a subtype of A” means “every B is an A.” In terms of specifications: “every B satisfies the specification for A.”

That means B is only a subtype of A if B’s specification is at least as strong as A’s specification. When we declare a class that implements an interface, the Java compiler enforces part of this requirement automatically: for example, it ensures that every method in A appears in B, with a compatible type signature. Class B cannot implement interface A without implementing all of the methods declared in A.

But the compiler cannot check that we haven’t weakened the specification in other ways: strengthening the precondition on some inputs to a method, weakening a postcondition, weakening a guarantee that the interface abstract type advertises to clients. If you declare a subtype in Java — implementing an interface is our current focus — then you must ensure that the subtype’s spec is at least as strong as the supertype’s.

reading exercises

Immutable shapes

Let’s define an interface for rectangles:

/** An immutable rectangle. */
public interface ImmutableRectangle {
    /** @return the width of this rectangle */
    public int getWidth();
    /** @return the height of this rectangle */
    public int getHeight();

It follows that every square is a rectangle:

/** An immutable square. */
public class ImmutableSquare {
    private final int side;
    /** Make a new side x side square. */
    public ImmutableSquare(int side) { this.side = side; }
    /** @return the width of this square */
    public int getWidth() { return side; }
    /** @return the height of this square */
    public int getHeight() { return side; }

Does ImmutableSquare.getWidth() satisfy the spec of ImmutableRectangle.getWidth()?

Does ImmutableSquare.getHeight() satisfy the spec of ImmutableRectangle.getHeight()?

Does the whole ImmutableSquare spec satisfy the ImmutableRectangle spec?

(missing explanation)

Mutable shapes
/** A mutable rectangle. */
public interface MutableRectangle {
    // ... same methods as above ...
    /** Set this rectangle's dimensions to width x height. */
    public void setSize(int width, int height);

Surely every square is still a rectangle?

/** A mutable square. */
public class MutableSquare {
    private final int side;
    // ... same constructor and methods as above ...
    // TODO implement setSize(..)

For each possible MutableSquare.setSize(..) implementation below, is it a valid implementation?

/** Set this square's dimensions to width x height.
 *  Requires width = height. */
public void setSize(int width, int height) { ... }

(missing explanation)

/** Set this square's dimensions to width x height.
 *  @throw BadSizeException if width != height */
public void setSize(int width, int height) throws BadSizeException { ... }

(missing explanation)

/** If width = height, set this square's dimensions to width x height.
 *  Otherwise, new dimensions are unspecified. */
public void setSize(int width, int height) { ... }

(missing explanation)

/** Set this square's dimensions to side x side. */
public void setSize(int side) { ... }

(missing explanation)

Example: MyString

Let’s revisit MyString. Using an interface instead of a class for the ADT, we can support multiple implementations:

/** MyString represents an immutable sequence of characters. */
public interface MyString { 

    // We'll skip this creator operation for now
    // /** @param b a boolean value
    //  *  @return string representation of b, either "true" or "false" */
    // public static MyString valueOf(boolean b) { ... }

    /** @return number of characters in this string */
    public int length();

    /** @param i character position (requires 0 <= i < string length)
     *  @return character at position i */
    public char charAt(int i);

    /** Get the substring between start (inclusive) and end (exclusive).
     *  @param start starting index
     *  @param end ending index.  Requires 0 <= start <= end <= string length.
     *  @return string consisting of charAt(start)...charAt(end-1) */
    public MyString substring(int start, int end);

We’ll skip the static valueOf method and come back to it in a minute. Instead, let’s go ahead using a different technique from our toolbox of ADT concepts in Java: constructors.

Here’s our first implementation:

public class SimpleMyString implements MyString {

    private char[] a;

    /** Create a string representation of b, either "true" or "false".
     *  @param b a boolean value */
    public SimpleMyString(boolean b) {
        a = b ? new char[] { 't', 'r', 'u', 'e' } 
              : new char[] { 'f', 'a', 'l', 's', 'e' };

    // private constructor, used internally by producer operations
    private SimpleMyString(char[] a) {
        this.a = a;

    @Override public int length() { return a.length; }

    @Override public char charAt(int i) { return a[i]; }

    @Override public MyString substring(int start, int end) {
        char[] subArray = new char[end - start];
        System.arraycopy(this.a, start, subArray, 0, end - start);
        return new SimpleMyString(subArray);

And here’s the optimized implementation:

public class FastMyString implements MyString {

    private char[] a;
    private int start;
    private int end;

    /** Create a string representation of b, either "true" or "false".
     *  @param b a boolean value */
    public FastMyString(boolean b) {
        a = b ? new char[] { 't', 'r', 'u', 'e' } 
              : new char[] { 'f', 'a', 'l', 's', 'e' };
        start = 0;
        end = a.length;

    // private constructor, used internally by producer operations.
    private FastMyString(char[] a, int start, int end) {
        this.a = a;
        this.start = start;
        this.end = end;

    @Override public int length() { return end - start; }

    @Override public char charAt(int i) { return a[start + i]; }

    @Override public MyString substring(int start, int end) {
        return new FastMyString(this.a, this.start + start, this.end + end);
  • Compare these classes to the implementations of MyString in Abstract Data Types. Notice how the code that previously appeared in static valueOf methods now appears in the constructors, slightly changed to refer to the rep of this.

  • Also notice the use of @Override. This annotation informs the compiler that the method must have the same signature as one of the methods in the interface we’re implementing. But since the compiler already checks that we’ve implemented all of the interface methods, the primary value of @Override here is for readers of the code: it tells us to look for the spec of that method in the interface. Repeating the spec wouldn’t be DRY, but saying nothing at all makes the code harder to understand.

  • And notice that we have added a private constructor for producers like substring(..) to use to make new instances of the class. Its arguments are the rep fields. We didn’t have to write this special constructor before, because Java provides an empty constructor by default when we don’t declare any other constructors. Adding the constructors that take boolean b means we have to declare another constructor explicitly for producer operations to use.

How will clients use this ADT? Here’s an example:

MyString s = new FastMyString(true);
System.out.println("The first character is: " + s.charAt(0));

This code looks very similar to the code we write to use the Java collections classes:

List<String> s = new ArrayList<String>();

Unfortunately, this pattern breaks the abstraction barrier we’ve worked so hard to build between the abstract type and its concrete representations. Clients must know the name of the concrete representation class. Because interfaces in Java cannot contain constructors, they must directly call one of the concrete class’ constructors. The spec of that constructor won’t appear anywhere in the interface, so there’s no static guarantee that different implementations will even provide the same constructors.

Fortunately, (as of Java 8) interfaces are allowed to contain static methods, so we can implement the creator operation valueOf as a static factory method in the interface MyString:

public interface MyString { 

    /** @param b a boolean value
     *  @return string representation of b, either "true" or "false" */
    public static MyString valueOf(boolean b) {
        return new FastMyString(true);

    // ...

Now a client can use the ADT without breaking the abstraction barrier:

MyString s = MyString.valueOf(true);
System.out.println("The first character is: " + s.charAt(0));

reading exercises

Code review

Let’s review the code for FastMyString. Which of these are useful criticisms:

The abstraction function should be documented

(missing explanation)

The representation invariant should be documented

(missing explanation)

The rep fields should be final so they cannot be reassigned

(missing explanation)

The private constructor should be public so clients can use it to construct their own arbitrary strings

(missing explanation)

The charAt specification should not expose that the rep contains individual characters

(missing explanation)

The charAt implementation should behave more helpfully when i is greater than the length of the string

(missing explanation)

Example: Generic Set<E>

Java’s collection classes provide a good example of the idea of separating interface and implementation.

Let’s consider as an example one of the ADTs from the Java collections library, Set. Set is the ADT of finite sets of elements of some other type E. Here is a simplified version of the Set interface:

/** A mutable set.
 *  @param <E> type of elements in the set */
public interface Set<E> {

Set is an example of a generic type: a type whose specification is in terms of a placeholder type to be filled in later. Instead of writing separate specifications and implementations for Set<String>, Set<Integer>, and so on, we design and implement one Set<E>.

We can match Java interfaces with our classification of ADT operations, starting with a creator:

    // example creator operation
    /** Make an empty set.
     *  @param <E> type of elements in the set
     *  @return a new set instance, initially empty */
    public static <E> Set<E> make() { ... } 

The make operation is implemented as a static factory method. Clients will write code like:
Set<String> strings = Set.make();
and the compiler will understand that the new Set is a set of String objects. (We write <E> at the front of this signature because make is a static method. It needs its own generic type parameter, separate from the E we’re using in instance method specs.)

    // example observer operations

    /** Get size of the set.
     *  @return the number of elements in this set */
    public int size();

    /** Test for membership.
     *  @param e an element
     *  @return true iff this set contains e */
    public boolean contains(E e);

Next we have two observer methods. Notice how the specs are in terms of our abstract notion of a set; it would be malformed to mention the details of any particular implementation of sets with particular private fields. These specs should apply to any valid implementation of the Set ADT.

    // example mutator operations

    /** Modifies this set by adding e to the set.
     *  @param e element to add */
    public void add(E e);

    /** Modifies this set by removing e, if found.
     *  If e is not found in the set, has no effect.
     *  @param e element to remove */
    public void remove(E e);

The story for these mutators is basically the same as for the observers. We still write specs at the level of our abstract model of sets.

reading exercises

Collection interfaces & implementations

Assume the following lines of code are run in sequence, and that any lines of code that don’t compile are simply commented out so that the rest of the code can compile.

The code uses two methods from Collections, so you might need to consult their documentation.

Choose the most specific answer to each question.

Set<String> set = new HashSet<String>();

set now points to:

(missing explanation)

set = Collections.unmodifiableSet(set);

set now points to:

(missing explanation)

set = Collections.singleton("glorp");

set now points to:

(missing explanation)

set = new Set<String>();

set now points to:

(missing explanation)

List<String> list = set;

set now points to:

(missing explanation)

Implementing Generic Interfaces

Suppose we want to implement the generic Set<E> interface above. We can either write a non-generic implementation that replaces E with a specific type, or a generic implementation that keeps the placeholder.

Generic interface, non-generic implementation. Let’s implement Set<E> for a particular type E.

In Abstraction Functions & Rep Invariants we looked at CharSet, which represents a set of characters. The example code for CharSet includes a generic Set interface and each of the implementations CharSet1/2/3 declare:

public class CharSet implements Set<Character>

When the interface mentions placeholder type E, the CharSet implementations replace E with Character. For example:

public interface Set<E> {

    // ...

     * Test for membership.
     * @param e an element
     * @return true iff this set contains e
    public boolean contains(E e);

     * Modifies this set by adding e to the set.
     * @param e element to add
    public void add(E e);

    // ...
public class CharSet1 implements Set<Character> {

    private String s = "";

    // ...

    public boolean contains(Character e) {
        return s.indexOf(e) != -1;

    public void add(Character e) {
        if (!contains(e)) s += e;
    // ...

The representations used by CharSet1/2/3 are not suited for representing sets of arbitrary-type elements. The String reps, for example, cannot represent a Set<Integer> without careful work to define a new rep invariant and abstraction function that handles multi-digit numbers.

Generic interface, generic implementation. We can also implement the generic Set<E> interface without picking a type for E. In that case, we write our code blind to the actual type that clients will choose for E. Java’s HashSet does that for Set. Its declaration looks like:

public interface Set<E> {

    // ...
public class HashSet<E> implements Set<E> {

    // ...

A generic implementation can only rely on details of the placeholder types that are included in the interface’s specification. We’ll see in a future reading how HashSet relies on methods that every type in Java is required to implement — and only on those methods, because it can’t rely on methods declared in any specific type.

Why Interfaces?

Interfaces are used pervasively in real Java code. Not every class is associated with an interface, but there are a few good reasons to bring an interface into the picture.

  • Documentation for both the compiler and for humans. Not only does an interface help the compiler catch ADT implementation bugs, but it is also much more useful for a human to read than the code for a concrete implementation. Such an implementation intersperses ADT-level types and specs with implementation details.
  • Allowing performance trade-offs. Different implementations of the ADT can provide methods with very different performance characteristics. Different applications may work better with different choices, but we would like to code these applications in a way that is representation-independent. From a correctness standpoint, it should be possible to drop in any new implementation of a key ADT with simple, localized code changes.
  • Methods with intentionally underdetermined specifications. An ADT for finite sets could leave unspecified the element order one gets when converting to a list. Some implementations might use slower method implementations that manage to keep the set representation in some sorted order, allowing quick conversion to a sorted list. Other implementations might make many methods faster by not bothering to support conversion to sorted lists.
  • Multiple views of one class. A Java class may implement multiple interfaces. For instance, a user interface widget displaying a drop-down list is natural to view as both a widget and a list. The class for this widget could implement both interfaces. In other words, we don’t implement an ADT multiple times just because we are choosing different data structures; we may make multiple implementations because many different sorts of objects may also be seen as special cases of the ADT, among other useful perspectives.
  • More and less trustworthy implementations. Another reason to implement an interface multiple times might be that it is easy to build a simple implementation that you believe is correct, while you can work harder to build a fancier version that is more likely to contain bugs. You can choose implementations for applications based on how bad it would be to get bitten by a bug.

reading exercises

Suppose you have an abstract data type for rational numbers, similar to the one we discussed in Abstraction Functions & Rep Invariants, which is currently represented as a Java class:

public class RatNum {

You decide to change RatNum to a Java interface instead, along with an implementation class called IntFraction:

public interface RatNum {

public class IntFraction implements RatNum {

For each piece of code below from the old RatNum class, identify it and decide where it should go in the new interface—plus—implementation-class design.

Interface + implementation 1
private int numer;
private int denom;

This piece of code is: (check all that apply)

It should be put in:

(missing explanation)

Interface + implementation 2
//   denom > 0
//   numer/denom is in reduced form

This piece of code is: (check all that apply)

It should be put in:

(missing explanation)

Interface + implementation 3
//   AF(numer, denom) = numer / denom

This piece of code is: (check all that apply)

It should be put in:

(missing explanation)

Interface + implementation 4
     * @param that another RatNum
     * @return a RatNum equal to (this / that)

This piece of code is: (check all that apply)

It should be put in:

(missing explanation)

Interface + implementation 5
    public boolean isZero()

This piece of code is: (check all that apply)

It should be put in:

(missing explanation)

Interface + implementation 6
    return numer == 0;

This piece of code is: (check all that apply)

It should be put in:

(missing explanation)


Sometimes an ADT has a small, finite set of immutable values, such as:

  • months of the year: January, February, …
  • days of the week: Monday, Tuesday, …
  • compass points: north, south, east, west
  • line caps in a line drawing: butt, round, square

A type like this may be used as part of a more complex type (like DateTime or Latitude), or as a parameter that changes the behavior of a method (like drawLine).

When the set of values is small and finite, it makes sense to define all the values as named constants, called an enumeration. Java has the enum construct to make this convenient:

public enum Month { JANUARY, FEBRUARY, MARCH, ..., DECEMBER };

This enum defines a new type name, Month, in the same way that class and interface define new type names. It also defines a set of named values, which we write in all-caps because they are effectively public static final constants. So you can now write:

Month thisMonth = MARCH;

This idea is called an enumeration because you are explicitly listing the elements of the set, and Java is assigning numbers to them as their default rep values.

In the simplest use case for enumerations, the only operation you need is testing equality between values:

if (day.equals(SATURDAY) || day.equals(SUNDAY)) {
    System.out.println("It's the weekend");

You may see code like this as well, which uses == instead of equals():

if (day == SATURDAY || day == SUNDAY) {
    System.out.println("It's the weekend");

If the day were represented as a String, this code would be very unsafe, because == tests whether two expressions refer to the same object at the same memory location, which may or may not be true for two arbitrary strings "Saturday". That’s why we always use equals() for comparing objects. But one of the advantages of using an enumeration type is that there is only ever one object in memory representing each value of the enumeration, and there is no way for a client to create more (no constructors!) So == is just as good as equals() for an enumeration.

In that sense, using an enumeration can feel like you’re using primitive int constants. Java even supports using them in switch statements (which otherwise only allow primitive integer types, not objects):

switch (direction) {
    case NORTH:  return "polar bears";
    case SOUTH:  return "penguins";
    case EAST:   return "elephants";
    case WEST:   return "llamas";

But unlike int values, enumerations have more static checking:

Month firstMonth = MONDAY; // static error: MONDAY has type DayOfWeek, not type Month 

An enum declaration can contain all the usual fields and methods that a class can. So you can define additional operations for the ADT, and define your own rep as well. Here is an example that has a rep, an observer, and a producer:

public enum Month {
    // the values of the enumeration.  Written as calls to the private constructor below.

    // rep
    private final int daysInMonth;

    // enums also have an automatic, invisible rep field:
    //   private final int ordinal;
    // which takes on values 0, 1, ... for each value in the enumeration.

    // rep invariant:
    //   daysInMonth is the number of days in this month in a non-leap year
    // abstraction function:
    //   AF(ordinal,daysInMonth) = the (ordinal+1)th month of the Gregorian calendar
    // safety from rep exposure:
    //   all fields are private, final, and have immutable types

    // Make a Month value.  Not visible to clients, only used to
    // initialize the constants above. 
    private Month(int daysInMonth) {
        this.daysInMonth = daysInMonth;

     * @param isLeapYear true iff the year under consideration is a leap year
     * @return number of days in this month in a normal year (if !isLeapYear) 
     *                                          or leap year (if isLeapYear)
    public int getDaysInMonth(boolean isLeapYear) {
        if (this == FEBRUARY && isLeapYear) {
            return daysInMonth+1;
        } else {
            return daysInMonth;

     * @return first month of the semester after this month
    public Month nextSemester() {
        switch (this) {
            case JANUARY:
                return FEBRUARY;
            case FEBRUARY:   // cases with no break or return
            case MARCH:      // fall through to the next case
            case APRIL:
            case MAY:
                return JUNE;
            case JUNE:
            case JULY:
            case AUGUST:
                return SEPTEMBER;
            case SEPTEMBER:
            case OCTOBER:
            case NOVEMBER:
            case DECEMBER:
                return JANUARY;
                throw new RuntimeException("can't get here");

All enum types also have some automatically-provided operations, defined by Enum:

  • ordinal() is the index of the value in the enumeration, so JANUARY.ordinal() returns 0.
  • compareTo() compares two values based on their ordinal numbers.
  • name() returns the name of the value’s constant as a string, e.g. JANUARY.name() returns "JANUARY".
  • toString() has the same behavior as name().

Read Enum Types (1 page) and Nested Classes (1 page) in the Java Tutorials.

reading exercises


Consider these three alternative ways to name the semester you’re registering for:

  • with a string literal:
startRegistrationFor("Fall", 2023);
  • with a named constant of type String:
public static final String FALL = "Fall";
startRegistrationFor(FALL, 2023);
  • with a value of an enumeration:
public enum Semester { IAP, SPRING, SUMMER, FALL };
startRegistrationFor(FALL, 2023);

Which of the following are correctly-stated advantages or disadvantages of each approach?

(missing explanation)

Realizing ADT Concepts in Java

We’ve now completed our Java toolbox of ADT concepts from the first ADTs reading:

ADT concept

Ways to do it in Java


Abstract data type

Single class


Interface + class(es)

List and ArrayList



Creator operation



Static (factory) method

Collections.singletonList(), Arrays.asList()



Observer operation

Instance method


Static method


Producer operation

Instance method


Static method


Mutator operation

Instance method


Static method



private fields


Java interfaces help us formalize the idea of an abstract data type as a set of operations that must be supported by a type.

This helps make our code…

  • Safe from bugs. An ADT is defined by its operations, and interfaces do just that. When clients use an interface type, static checking ensures that they only use methods defined by the interface. If the implementation class exposes other methods — or worse, has visible representation — the client can’t accidentally see or depend on them. When we have multiple implementations of a data type, interfaces provide static checking of the method signatures.

  • Easy to understand. Clients and maintainers know exactly where to look for the specification of the ADT. Since the interface doesn’t contain instance fields or implementations of instance methods, it’s easier to keep details of the implementation out of the specifications.

  • Ready for change. We can easily add new implementations of a type by adding classes that implement interface. If we avoid constructors in favor of static factory methods, clients will only see the interface. That means we can switch which implementation class clients are using without changing their code at all.

Java enumerations allow defining an ADT with a small finite set of immutable values. Compared to the old-fashioned alternatives of special integer values or special strings, enumerations help make our code:

  • Safe from bugs. Static checking ensures that clients can’t use values outside the finite set, or confuse two different enumerated types.

  • Easy to understand. Named constants are not as magical as integer literals, and named types explain themselves better than int or String.

  • Ready for change. Enumerations have no particular advantage here.