An interface is similar to a pure abstract class. It does not include attribute definitions and all its methods are abstract methods. Constant definitions are allowed. An interface does not include constructors and cannot be instantiated.
To define an interface, the keyword interface must be used instead of abstract class and before the name of the interface. For the functions, the keyword abstract is not needed because all the functions are abstract functions. In a similar manner, all features of an interface are implicitly public. An interface definition in KJP has the following general structure:
description . . . interface 〈 interface_name 〉 is public constants . . . // public operations . . . endinterface 〈 interface_name 〉
The following interface, named Iball, defines the specification for the behavior of objects of any class that implements this interface. Note that the structure of the interface is very similar to that of an abstract class.
description This is a simple interface using KJP */ interface Iball is public description This method accesses the value of attribute color */ function get_color of type integer description This method reads the value of attribute size from the console. */ function get_size of type real description This function returns the value of the move_status. */ function get_m_status of type character description This function displays the color, status, and size of the object. */ function show_state description This function changes the move_status of the object to move. */ function move description This function changes the move_status of the object to stop. */ function stop endinterface Iball
A class makes use of an interface by implementing it. All methods declared in the interface must be implemented in the class that implements it. This class can define additional features. The class that implements an interface must use KJP statement implements. The header for the class definition that uses this KJP statement has the following general structure:
description . . . class 〈 cls_name 〉 implements 〈 interface_name 〉 is . . . endclass 〈 cls_name 〉
On the CD | For example, class Nball implements interface Iball, which was defined earlier. The KJP code for this class follows and is stored in the file Nball.kpl. |
// Simple class using KJP // Jan 2003, J Garrido // description This class implements an interface. */ class Nball implements Iball is private // attributes variables integer color character move_status real size public // public methods description Constructor initializes the state. */ function initializer parameters integer icolor, real isize is begin set color = icolor set move_status = 'S' set size = isize endfun initializer description This method accesses the value of attribute color. */ function get_color of type integer is begin return color endfun get_color description This method accesses the value of attribute size. */ function get_size of type real is begin return size endfun get_size description This function returns the value of the move_status. */ function get_m_status of type character is begin return move_status endfun get_m_status description This function displays the color, status, and size of the object. */ function show_state is begin display "Color of ball object: ", color display "Size of ball object: ", size display "Status of ball object: ", move_status endfun show_status description This function changes the move_status of the object to move. */ function move is begin set move_status = 'M' endfun move description This function changes the move_status of the object to stop */ function stop is begin set move_status = 'S' endfun stop endclass Nball
One of the significant differences between an interface and an abstract class is that multiple interfaces can be implemented by a class, whereas only one abstract base class can be inherited by a subclass. Another important difference is that no data attribute definitions are allowed in an interface, only constant definitions. It is mandatory for a class that implements an interface to implement all the functions defined in the interface.