This chapter explains the rules for writing interfaces, with short examples of code. For full syntax definitions, see your Language Reference.
An interface is a set of method prototypes; that is, skeleton methods with no code. Classes can implement interfaces; a class that implements an interface must provide full method definitions for the method prototypes in the interface.
You can develop a hierarchy of inheriting interfaces, similar to a hierarchy of classes. An interface can inherit from more than one interface; this is known as multiple inheritance. An inheriting interface has all the method specifications defined for the inherited interface definition or definitions, including any method specifications that the inherited definition or definitions inherited. The inheriting interface can define new methods augmenting the set of inherited method specifications. The inheriting interface must always conform to each of the inherited interfaces.
You write your interface definitions in interface source elements. An interface source element can contain one or more interface definitions.
The code block below shows the outline of an example interface source element. An ellipsis (...) shows where code has been omitted, and indentation indicates the levels of nesting. In this example the interface Drivable, containing a method prototype for calculating mileage, inherits from the interface Rentable, which can be assumed to contain methods applicable to all objects that can be rented.
interface-id. Drivable inherits from Rentable. *> Interface identification. environment division. *> Environment division *> header is optional. ... configuration section. repository. *> Repository paragraph *> names the interfaces this *> interface refers to. interface Drivable *> Information for the *> Drivable interface stored
*> in the external repository *> under the name "DRIVABLE" interface Rentable as "rble" *> Information for the *> Rentable interface stored *> in the external repository *> under the name "rble". . *> Period ends paragraph. procedure division. method-id. "calcMileage". *> Start of method *> prototype "calcMileage". *> Method contains data *> but no code. data division. linkage section. 01 endMileage pic 9(6). 01 beginMileage pic 9(6). 01 mileage pic 9(6). procedure division using endMileage, beginMileage returning mileage. end method "calcMileage". *> End of method prototype. method-id. "logDamage". *> Start of method *> prototype "logDamage". end method "logDamage". *> End of method prototype. end interface Drivable.
Factory objects and instance objects within classes can implement interfaces. The implementing object implements all the method prototypes defined for the implemented interface definition or definitions, including any method prototypes that the implemented definition or definitions inherited.
Here is an example of an instance object that implements the Drivable interface:
class-id. Car inherits from Vehicle. configuration section. repository. class Car class Vehicle interface Drivable . factory. * The code for the factory object starts here ... * The code for the factory object ends here. end factory. *-----The instance object code starts here. object. implements Drivable. working-storage section. *> Instance data goes here. ... procedure division. interface-methods. *> Start of implementation *> of interface methods. method-id. "calcMileage". data division. linkage section. 01 endMileage pic 9(6). 01 beginMileage pic 9(6). 01 mileage pic 9(6). procedure division using endMileage, beginMileage returning mileage. subtract endMileage from beginMileage giving mileage. end method "calcMileage". method-id. "logDamage". ... end method "logDamage". method-id. "pickUp" *> Implementation of method ... *> inherited by Drivable from end method "pickUp" *> Rentable instance methods. *> start of instance methods *> for an object of this class ... end object. *-----the instance object code ends here. end class Car.
The Compiler provides conformance checking for interface implementation. It checks that the interface of the factory object of the implementing class conforms to the interfaces to be implemented by the factory object, and that the interfaces of the instance objects of the implementing class conform to the interfaces to be implemented by the instance object. The Compiler also checks that you have implemented all the methods listed in the interface definition. For more information about conformance see the section Conformance in the chapter Using Objects in Programs.
A parameterized interface is a skeleton interface that has one or more formal parameters. When you provide specific class names or interface names as actual parameters, the Compiler expands the parameterized interface into a new non-parameterized interface. The purpose of parameterized interfaces is to make it easier for you to create many similar interfaces.
A parameterized interface always has a USING phrase that lists the formal parameters in its Interface-ID paragraph.
You need to specify the EXPANDS phrase in the entry for the interface in the Repository paragraph in any program that uses a interface created from a parameterized interface.
Here is an outline of the code for a parameterized interface:
interface-id. pinterface using fparam1 fparam2. repository. class fparam1 class fparam2. method-id meth1. procedure division using by reference fparam1. end method meth1. method-id meth2. procedure division using by reference fparam2. end method meth1. end interface pinterface.
At compile time all occurrences of fparam1 and fparam2 are replaced by the names of actual classes or interfaces. To make this happen you need to specify the ACTUAL-PARAMS Compiler directive. In this example the directive might be:
ACTUAL-PARAMS(MyInterface ClassA ClassB)
MyInterface is the new interface to create from the parameterized interface. ClassA and ClassB are the class names that replace fparam1 and fparam2.
You can now use the MyInterface interface in a class, for example:
class-id. ClassC. configuration section. repository. interface MyInterface expands pinterface using ClassA ClassB ... factory. implements MyInterface. ... stop run
You can use the MyInterface interface in a procedural program too, but only indirectly. You need to create a class that implements the interface, then include entries for both the interface and the class in the program's Repository paragraph. In the program you can use typed object references of the MyInterface interface, and use the myInterface interface in object views, for example:
program-id. prog. repository. class A. class B. class ClassC implements MyInterface. *> this class implements MyInterface interface MyInterface expands pinterface using ClassA ClassB. ... working-storage section. 1 obj-ref1 usage object reference MyInterface. 1 obj-ref2 usage object reference factory class3. procedure division. set obj-ref1 to obj-ref2 invoke obj-ref1 "someMethodInTheInterface" using ...
Copyright © 2004 Micro Focus International Limited. All rights reserved.