6.2 Interfaces
definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The body of an interface form has interface clauses that are similar to class clauses, but declared separately and sometimes with different syntax. For example, extends as an interface clause supports multiple superinterface names instead of just one, and extends can appear multiple times in an interface body.
Interfaces cannot be instantiated. They are implemented by classes via the implements form. When a class implements an interface (not privately or protectedly), it has all methods and properties of the interface, and its instances satisfy the interface as an annotation.
Typically, an interface declares methods and properties with abstract to be implemented by classes that implement the interface. However, an interface can define method and property implementations; those implementations are inherited by classes that implement the interface or any subinterface that extends the interface. An interface can also have private helper methods and properties, but they are useful only when an interface also has implemented public or protected methods or properties that refer to them.
When a class implements an interface privately using private implements or protectedly using or protected implements, its instances do not satisfy the interface as an annotation. If the privately or protectedly implemented interface has an internal name declared with internal, however, instances satisfy the internal name as an annotation. Methods and properties of a privately or protectedly implemented instance can be accessed only with static . via the internal-name annotation. As long as a method or property belongs to only to privately implemented interfaces, it can be overridden with private override, otherwise it is overridden normally. Methods of a protectedly implemented interface are treated as protected in the implementing class, even when the methods are declared as non-protected in the interfaces. If a class declares the implementation of a interface both normally and privately or protectedly, then the interface is implemented normally; if an interface is declared as implemented both privately and protectedly, then it is protectedly implemented. Abstract private methods and properties must be implemented immediately in the class that privately implements the associated interface.
When a class or interface extends or implements multiple interfaces that provide a method or property with the same name, the method or property implementation must be the same for all interfaces. That is, the method or property must be abstract, the implementation must reside in a shared superinterface of the interfaces, or the method or property must be overridden in the implementing class. Overriding applies to same-named methods or properties of all interfaces.
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
A method, override, or property declaration can be just an identifier, or it can omit a body block. In that case, method, override, or property is treated as if abstract is added before. If arguments are declared for an abstract method, they determine the method’s expectations for static argument-count checking (see use_static), but they do not impose constraints on overriding implementations. When a property_decl uses the single-case | form, it declares the property as not supporting assignment; that declaration is not enforced on implementations of the property, but it affects static resolution of a property assignment.
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
interface clause | |
| |
interface clause | |
| |
interface clause | |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
| |
interface clause | |
interface clause | |
| |
| |
interface clause | |
|
There is no constructor for interfaces, since interfaces cannot be instantiated directly, but an expression clause can make an interface identifier behave like a constructor, perhaps instantiating some default class. There is no binding for interfaces, because interface does not otherwise define an interface name for binding, and so bind.macro can be used alongside interface with the same interface name.
interface clause | |
|
The expr and body are evaluated in order relative to surrounding expr and defn forms.
See also the primitive_property class clause form.