Note: information on this page refers to Ceylon 1.0, not to the current release.
Interfaces
An interface is a stateless type that, unlike a class:
- may not hold references to other objects,
- does not define initialization logic, and
- cannot be directly instantiated.
Interfaces support a more flexible inheritance model: multiple inheritance. "Diamond" inheritance is not a problem for interfaces, because interfaces don't have state or logic to initialize that state.
Usage
A trivial interface declaration looks like this:
interface I {
/* declarations of interface members */
}
Since interfaces don't contain initialization logic, there's no parameter list after the interface name as there would be in a class declaration.
Description
Satisfying interfaces
The satisfies
keyword specifies the interfaces inherited by an
interface:
interface I satisfies I1 & I2 {
/* declarations of interface members */
}
If an interface is declared without using the satisfies
keyword,
it does not directly inherit any interfaces. However, all
interfaces are considered to inherit the class
Object
.
&
is used as the separator between satisfied interfaces because
I
is being defined as a subtype of the
intersection type I1&I2
.
Enumerated interfaces
The subclasses of an enumerated interface can be constrained to a
list of named interfaces, classes, or toplevel anonymous classes
using the of
clause. If the interface I
is permitted only two
direct subtypes, the interface I1
, and the class C1
, its
declaration would look like this:
interface I of I1 | C1 {
/* declarations of interface members */
}
Generic interfaces
An generic interface declaration lists type parameters
in angle brackets (<
and >
) after the interface name.
interface I<Z> {
/* declarations of interface members
type parameter Z treated as a type */
}
An interface declaration with type parameters may have a given
clause for each declared type parameter to
constrain the argument types.
shared
interfaces
A toplevel interface declaration, or an interface declaration nested
inside the body of a containing class or interface, may be annotated
shared
:
shared class C() {
/* declarations of class members */
}
- A toplevel
shared
interface is visible wherever the package that contains it is visible. - A
shared
interface nested inside a class or interface is visible wherever the containing class or interface is visible.
Members
The permitted members of interfaces are classes, interfaces, methods, and attributes.
Note that an interface cannot have an object
member.
Aliases
An interface alias is a kind of alias.
Metamodel
Interface declarations can be manipulated at runtime via their representation as
InterfaceDeclaration
instances. An applied interface (i.e. with all type parameters specified) corresponds to
either an
Interface
or
MemberInterface
model instance.
See also
- Classes
- Interfaces in the Ceylon language spec