Extending Interfaces



next up previous
Next: Implementations Up: Language Extensions Previous: Satisfying Where Clauses

Extending Interfaces

In Java, interface and class definitions explicitly indicate their position in the type hierarchy. Parameterized definitions are no different. For example, the interface SortedList_Member given in Figure 3 extends SortedList by providing a membership test via the member method. Note that the where clause for this interface is more restrictive than SortedList's: the notion of membership requires an equality test (equals), and SortedList_Member objects also order their objects using lt.


interface SortedList_Member [T]
    where T { boolean lt (T); boolean equals (T); }
    extends SortedList[T] {

// overview:  SortedList_Member's are sorted lists
// with a membership test.

    boolean member (T x);
    // effects:  returns true if x is in this else returns false.

}

Figure 3: Extending a parameterized interface

The rule for the legality of an extension is particularly simple: An extension is legal if all the extended class or interface definitions are legal. The extended type must be a class or interface; it may not be a formal parameter. For example, the definition of SortedList_Member states that it extends SortedList[T]. Since the formal parameter T satisfies the lt where clause of SortedList, the declaration is legal.

The meaning of the extends clause is the following:

For all types t where SortedList_Member[t] is a legal instantiation, SortedList_Member[t] extends SortedList[t]

Thus, SortedList_Member[int] extends SortedList[int], etc. If SortedList_Member[t] is legal, then we can be sure that SortedList[t] is also legal because of the type checking of the instantiation of SortedList when the SortedList_Member interface was compiled.

The extends clause need not use all the parameters of the interface being defined. Here are three examples:


    interface A[T] extends B
    interface Map[T, S] extends Set[T]
    interface StringMap[T] extends Map[String, T]
In the first example, the declaration states that for any actual t, A[t] extends B. In other words, anywhere that a B is allowed, an A[t] can be used with any t (provided the instantiation is legal). In the second example, the declaration states that for any actual types t and s, Map[t,s] extends Set[t]. The final example shows that some parameters of the interface being extended may be supplied with actual types.

As in ordinary Java, all subtype relations for instantiations must be explicitly declared. The same rule holds for classes as well, except that Object is automatically a supertype of all class types. In particular, subtype relations between different instantiations of the same parameterized class are disallowed. Thus SortedList[BigNum] is not a subtype of SortedList[Num], even if BigNum is a subtype of Num. The reasons for this rule are discussed in Section 4.2 and elsewhere [DGLM95].



next up previous
Next: Implementations Up: Language Extensions Previous: Satisfying Where Clauses

Andrew C. Myers, Joseph A. Bank, Barbara Liskov
Copyright © 1996 Association for Computing Machinery