Interfaces, as specified in Partition I, section 8.9.4, define a contract that other types may implement. Interfaces may have static fields and methods, but they shall not have instance fields or methods. Interfaces may define virtual methods, but only if they are abstract (see Partition I, section 8.9.4 and Partition II, section 14.4.2.4). RATIONALE Interfaces cannot define instance fields for the same reason that the CLI does not support multiple inheritance of base types: in the presence of dynamic loading of data types there is no known implementation technique that is both efficient when used and has no cost when not used. By contrast, providing static fields and methods need not affect the layout of instances and therefore does not raise these issues. Interfaces may be nested inside any type (interface, class, or value type). Classes and value types shall not be nested inside of interfaces. 11.1 Implementing InterfacesClasses and value types shall implement zero or more interfaces. Implementing an interface implies that all concrete instances of the class or value type shall provide an implementation for each abstract virtual method declared in the interface. In order to implement an interface, a class or value type shall either explicitly declare that it does so (using the implements attribute in its type definition, see Partition II, section 9.1) or shall be derived from a base class that implements the interface. NOTE An abstract class (since it cannot be instantiated) need not provide implementations of the virtual methods of interfaces it implements, but any concrete class derived from it shall provide the implementation. Merely providing implementations for all of the abstract methods of an interface is not sufficient to have a type implement that interface. Conceptually, this represents the fact that an interface represents a contract that may have more requirements than are captured in the set of abstract methods. From an implementation point of view, this allows the layout of types to be constrained only by those interfaces that are explicitly declared. Interfaces shall declare that they require the implementation of zero or more other interfaces. If one interface, A, declares that it requires the implementation of another interface, B, then A implicitly declares that it requires the implementation of all interfaces required by B. If a class or value type declares that it implements A, then all concrete instances shall provide implementations of the virtual methods declared in A and all of the interfaces A requires. Example (informative): The following class implements the interface IStartStopEventSource defined in the module Counter. .class private auto autochar StartStopButton extends [System.Windows.Forms]System.Windows.Forms.Button implements [.module Counter]IstartStopEventSource { // body of class } 11.2 Implementing Virtual Methods on InterfacesClasses that implement an interface (see Partition II, section 11.1) are required to provide implementations for the abstract virtual methods defined by the interface. There are three mechanisms for providing this implementation:
The Virtual Execution System shall determine the appropriate implementation of a virtual method to be used for an interface abstract method using the following algorithm.
RATIONALE Interfaces can be thought of as specifying, primarily, a set of virtual methods that shall be implemented by any class that implements the interface. The class specifies a mapping from its own virtual methods to those of the interface. Thus it is virtual methods, not specific implementations of those methods, that are associated with interfaces. Overriding a virtual method on a class with a specific implementation will thus affect not only the virtual method named in the class, but also any interface virtual methods to which that same virtual method has been mapped.
|