Following is the general format to create an interface −
INTERFACE <intf_name>.
DATA.....
CLASS-DATA.....
METHODS.....
CLASS-METHODS.....
ENDINTERFACE.
In this syntax, <intf_name> represents the name of an interface. The DATA and CLASSDATA statements can be used to define the instance and static attributes of the interface respectively. The METHODS and CLASS-METHODS statements can be used to define the instance and static methods of the interface respectively. As the definition of an interface does not include the implementation class, it is not necessary to add the DEFINITION clause in the declaration of an interface.
Note − All the methods of an interface are abstract. They are fully declared including their parameter interface, but not implemented in the interface. All the classes that want to use an interface must implement all the methods of the interface. Otherwise, the class becomes an abstract class.
We use the following syntax in the implementation part of the class −
INTERFACE <intf_name>.
In this syntax, <intf_name> represents the name of an interface. Note that this syntax must be used in the public section of the class.
The following syntax is used to implement the methods of an interface inside the implementation of a class −
METHOD <intf_name~method_m>.
<statements>.
ENDMETHOD.
In this syntax, <intf_name~method_m> represents the fully declared name of a method of the <intf_name> interface.
Report ZINTERFACE1.
INTERFACE my_interface1.
Methods msg.
ENDINTERFACE.
CLASS num_counter Definition.
PUBLIC Section.
INTERFACES my_interface1.
Methods add_number.
PRIVATE Section.
Data num Type I.
ENDCLASS.
CLASS num_counter Implementation.
Method my_interface1~msg.
Write: / 'The number is', num.
EndMethod.
Method add_number.
ADD 7 TO num.
EndMethod.
ENDCLASS.
CLASS drive1 Definition.
PUBLIC Section.
INTERFACES my_interface1.
Methods speed1.
PRIVATE Section.
Data wheel1 Type I.
ENDCLASS.
CLASS drive1 Implementation.
Method my_interface1~msg.
Write: / 'Total number of wheels is', wheel1.
EndMethod.
Method speed1.
Add 4 To wheel1.
EndMethod.
ENDCLASS.
Start-Of-Selection.
Data object1 Type Ref To num_counter.
Create Object object1.
CALL Method object1→add_number.
CALL Method object1→my_interface1~msg.
Data object2 Type Ref To drive1.
Create Object object2.
CALL Method object2→speed1.
CALL Method object2→my_interface1~msg.
The above code produces the following output −
The number is 7
Total number of wheels is 4
In the above example, my_interface1 is the name of an interface that contains the 'msg' method. Next, two classes, num_counter and drive1 are defined and implemented. Both these classes implement the 'msg' method and also specific methods that define the behavior of their respective instances, such as the add_number and speed1 methods.
Note − The add_number and speed1 methods are specific to the respective classes.
73 videos|68 docs
|
|
Explore Courses for Software Development exam
|