@head @title Generic inheritance and generic interfaces @note This section is a stub. @end

This section explains how to inherit from a generic type and how to implement a generic interface. We illustrate these by expanding the Stack class defined in section @href{Defining a generic type}. @h2 Implementing a generic interface

Here is a modified version of the stack example that implements the @ref{Iterable} interface (differences are highlighted): @example class Stack<T> {{{implements Iterable<T>}}} private var array = [] as Array<T> def create() as void end {{{ def iterator() as Iterator<T> return self.array.iterator() end }}} def isEmpty() as Boolean return self.array == [] end def push(o as T) self.array.append(o) end def pop() as T self.array.removeAt(-1) end end @end @h2 Inheriting from a generic class

We can define subclasses of a generic class such as Stack: @example class IntStack is Stack<Int> end class ArrayStack<S> is Stack<Array<S>> end @end

The IntStack class is a non-generic class that inherits Stack; it fixes the type variable T to Int. IntStack is compatible with Stack<Int>. ArrayStack also inherits Stack, but it is generic like Stack. ArrayStack<Int> is compatible with Stack<Array<Int>> (and similarly for other values of type variable S).

Note that a generic subclass may not define additional type variables not present in the superclass (unless the superclass is Object). Each type variable in a subclass must be mapped to a superclass type variable. The following class is not valid, since the subclass defines two type variable while the superclass only has one: @example class TupleStack<T1, T2> is Stack<(T1, T2)> end @end

This restriction does not apply to interface inheritance. @h2 Generic inheritance and mixed typing

You can also inherit a dynamically-typed class from a generic class: @example class DynStack is Stack def length() return Array(iterator()).length() end end @end

In this case the type variable T of Stack defaults to dynamic for instances of DynStack.