INTERFACE
An interface is defined by a list of methods for a type.
Syntax
INTERFACE
method-name (
parameter-name data-type
[,...]
)
[ RETURNS { data-type
| ( [ data-type [,...] ] )
} ]
[,...]
END INTERFACE
- method-name defines the name of a method.
- parameter-name is the name of a formal argument of the method.
- data-type can be a primitive data type, a user defined type, a built-in class, an imported package class, or a Java class.
Usage
An INTERFACE
structure defines a list of methods that apply to types defined as
records with methods. The interface defines the
how, the type defines the what.
An interface is associated to a record type through the list of methods defined for that type.
For example, a
Rectangle
record type defining an area()
method can
be associated to an interface defining that method:TYPE Rectangle RECORD
height, width FLOAT
END TYPE
PUBLIC FUNCTION (r Rectangle) area() RETURNS FLOAT
RETURN r.height * r.width
END FUNCTION
All elements inside an
INTERFACE
must be and can only be methods for a user-defined type, and must be specified in
the interface by using the same parameter names, parameter types and return types of the methods it
refers to. An interface is typically defined as a TYPE
, to simplify its
reusage:TYPE Shape INTERFACE
area() RETURNS FLOAT,
kind() RETURNS STRING
END INTERFACE
Methods of multiple individual types associated to an interface can be invoked indirectly by
declaring a variable with the interface structure:
DEFINE s Shape, x FLOAT
LET x = s.area() -- Can be the area() method for types Circle, Rectangle, etc.
Before it can be used, an interface variable must be set by referencing an instance of a another
variable defined with a corresponding record
type:
DEFINE r Rectangle, s Shape, x FLOAT
LET s = r
LET x = s.area()
Several interfaces can be defined for a given type. This provides a high level of
flexibility:
TYPE Shape INTERFACE
area() RETURNS FLOAT
END INTERFACE
TYPE Domain INTERFACE
domainName() RETURNS STRING
END INTERFACE
...
DEFINE r Rectangle = ( height:10, width:20 )
DEFINE v1 Shape
DEFINE v2 Domain
LET v1 = r
DISPLAY v1.area()
LET v2 = r
DISPLAY v2.domainName()