Use an interface definition
- Last Updated: March 30, 2020
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
Interfaces cannot be instantiated, but they can be used
as the data type to define an object reference data element, such
as in a DEFINE VARIABLE statement or a DEFINE PARAMETER statement.
This data element can then be assigned to hold an object reference
to an instance of a class that implements the interface. Multiple
classes can implement the same interface, which allows the classes
to be treated in a common manner. An object reference to an interface
can then be used to call these common methods, even though the underlying
object might be of different classes. This is similar to having
multiple classes inheriting from the same super class (with the
exception that an interface itself provides no method implementation).
Thus, an object reference to a super class can also be used to call
the methods that are defined in the super class, even though the underlying
object might be of different subclasses with varying method implementations.
The following example shows a simple interface definition, which
is modified from the sample acme.myObjs.Interfaces.IBusObj interface with
an additional Name property:
|
Note that this interface declares two overloadings of the printObj( ) method.
The following fragment from the acme.myObjs.CustObj sample
class shows how it might implement this interface, extended with
the additional Name property:
|
Note that while a property interface declaration may include a GET and SET accessor, or a GET accessor, or a SET accessor, you cannot force the property to not have a particular accessor in an implementing class. However, you can force the accessor to be missing when an instance of the class is used through an interface reference. You can do this by omitting the accessor in the property interface definition. Even though the accessor might be implemented in the class property, when used through an interface reference it will appear that the accessor implementation does not exist.