On this page:
interface
extends
final
method
property
override
private
protected
abstract
internal
expression
annotation
primitive_  property
8.15.0.12

6.2 Interfaces🔗ℹ

definition

interface id_name

 

definition

interface id_name:

  interface_clause_or_body_or_export

  ...

 

interface_clause_or_body_or_export

 = 

interface_clause

 | 

body

 | 

export

 

interface_clause

 = 

method method_impl

 | 

override method_impl

 | 

final method_impl

 | 

private method_impl

 | 

protected method_impl

 | 

abstract method_decl

 | 

property property_impl

 | 

extends extends_decl

 | 

internal internal_decl

 | 

expression expression_decl

 | 

annotation annotation_decl

 | 

dot dot_decl

 | 

static_info static_info_decl

 | 

primitive_property primitive_property_decl

 | 

other_interface_clause

Similar to class for defining classes, but defines an interface, which has no fields but can have multiple superinterfaces. A method, property, or override clause is allowed to have just a name or omit the body, in which case abstract implicitly prefixes the declaration.

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

extends id_name

 

interface clause

extends: id_name ...; ...

An interface clause recognized by interface to define an interface that is a subinterface of the ones named by the id_names.

interface clause

final method_impl

 

interface clause

final method method_impl

 

interface clause

final override method_impl

 

interface clause

final override method method_impl

 

interface clause

final override method method_impl

 

interface clause

final property property_impl

 

interface clause

final override property property_impl

 

interface clause

final protected method_impl

 

interface clause

final protected method method_impl

 

interface clause

final protected property method_impl

Like the final class clause form, but as an interface clause.

interface clause

method method_decl

 

interface clause

method method_impl

 

interface clause

property property_decl

 

interface clause

property property_impl

 

interface clause

override method_impl

 

interface clause

override method_decl

 

interface clause

override method method_impl

 

interface clause

override method method_decl

 

interface clause

override property property_impl

 

interface clause

override property property_decl

These interface clauses are recognized by interface to declare methods and properties, and they are analogous to the method, property, and override

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

private method_impl

 

interface clause

private method method_impl

 

interface clause

private property property_impl

 

interface clause

private override method_impl

 

interface clause

private override method method_impl

 

interface clause

private override property property_impl

An interface clause that declares a private method or property. See interface and method for more information on method and property declarations. A private without method, override, or property is equivalent to private followed by method.

interface clause

protected method_impl

 

interface clause

protected method method_impl

 

interface clause

protected property property_impl

Like private, but protected methods and and properties, can be referenced and overridden within subclasses, subinterfaces, and subveneers. An overriding declaration does not use protected again.

interface clause

abstract method_decl

 

interface clause

abstract method method_decl

 

interface clause

abstract override method_decl

 

interface clause

abstract property property_decl

 

interface clause

abstract override property property_decl

 

interface clause

abstract protected method_decl

 

interface clause

abstract protected method method_decl

 

interface clause

abstract protected property property_decl

An interface clause that declares a method or property without an implementation, analogous to abstract for classes. Methods and properties of an interface can be defined as abstract without the abstract form by declaring the method or property without an implementation.

interface clause

internal id

 

interface clause

internal: id

An interface clause recognized by interface to bind id to the interface’s representation. See interface and the internal class clause for more information.

interface clause

expression: expression_decl

 

interface clause

annotation: annotation_point

These interface clause forms have the same syntax and analogous meaning as the expression and annotation class clauses.

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

primitive_property expr: body; ...

An interface clause that bridges Rhombus and Racket protocols. Any class that implements the interface will implement the primitive property with the body value.

The expr and body are evaluated in order relative to surrounding expr and defn forms.

See also the primitive_property class clause form.