Interfaces
An interface is a list properties that you can apply to a class. When you apply an interface to a class, that class must have all of the properties declared in the interface. The class can also have additional definitions that are not in the interface. The LIGO compiler warns you of any mismatches between the interface and the class.
For example, the following interface specifies that a class must have these contents:
- A property named
add, which accepts two values of typenatand returns a value of typenat - Values
oneandtwo, which are of the typenat
To apply an interface to a class, put the name of the interface after
the keyword implements and the class name, as in the following
example. We say that the class implements the interface. This
class defines the properties add, one and two. It also adds a
property named multiply that is not specified in the interface:
Note how properties from the interface must be defined as static in
the class.
Extending interfaces
Interfaces can be extended by inheritance with the extends keyword, as in this example:
Interfaces can extend more than one interface, which can lead to an interface that extends a base interface more than once, known as diamond inheritance. For example, the following interface extends two interfaces from the previous example. Because both of these interfaces extend the same base interface, it is as if the interface extends the base interface twice. Diamond inheritance doesn't cause any problems for the interface.
Interfaces can have optional types and values indicated with a
question mark ?. In the previous example, the interface
NewEuro_INTF has an optional property five_hundred. This class
defines this optional value and adds a property named twenty that is
not defined in the NewEuro_INTF interface: