11 Semantics of Interfaces


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 Interfaces

Classes 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 graphics/ccc.gif 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 Interfaces

Classes 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:

  • Directly specifying an implementation, using the same name and signature as appear in the interface

  • Inheritance of an existing implementation from the base type

  • Use of an explicit MethodImpl (see Partition II, section 14.1.4)

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.

  • If the parent class implements the interface, start with the same virtual methods that it provides; otherwise create an interface that has empty slots for all virtual functions.

  • If this class explicitly specifies that it implements the interface

    • If the class defines any public virtual newslot functions whose name and signature match a virtual method on the interface, then use these new virtual methods to implement the corresponding interface method.

  • If there are any virtual methods in the interface that still have empty slots, see if there are any public virtual methods available on this class (directly or inherited), and use these to implement the corresponding methods on the interface.

  • Apply all MethodImpls that are specified for this class, thereby placing explicitly specified virtual methods into the interface in preference to those inherited or chosen by name matching.

  • If the current class is not abstract and there are any interface methods that still have empty slots, then the program is not valid.

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.


ANNOTATION

Interface definitions contain only static methods and declarations of abstract virtual methods, so the interface definition never provides implementations of virtual methods. Instead, classes that implement the interface must always implement these virtual methods. This section describes the algorithm to determine which implementation to use for virtual interface methods when there is more than one implementation for any or all of the interface methods.

One subtle point is worth mentioning. After the first part of the algorithm, in which the machine fills the interface slots with the parent's implementation (if any), it then looks to see if the child class has any public virtual method implementations for a given name and signature but only if the child says it is implementing it. The child would do so by declaring the method in its type declaration, as specified in Partition II, sections 9.1 and 9.1.3. If the child class does not explicitly say it is implementing the interface, then the parent implementation is used.

Most programming languages, with the exception of C#, make no distinction between whether a class explicitly says that it implements an interface, or simply derives from a class that implements the interface. A complete understanding of this algorithm gives great flexibility to compiler writers. If you really understand this algorithm, you can use it to implement several different ways of populating interfaces.




The Common Language Infrastructure Annotated Standard (Microsoft. NET Development Series)
The Common Language Infrastructure Annotated Standard (Microsoft. NET Development Series)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net