Interfaces
A single-inheritance model avoids these kinds of vagaries because a subclass
always inherits from a single superclass. However, interfaces can enhance this
model by providing a way to extend the type of a class without having to bring
along all of the implementation baggage associated with multiple inheritance. In
Section 6.3.4, Working with Interfaces, you will see how the implementation of
an interface allows a class to be used polymorphically wherever a reference of
that interface type is used.
6.3.2 Defining Interfaces
The syntax required to define an interface is veiy similar to the syntax that is used
in the declaration part of
a
class definition. Listing 6.7 shows an example of how
to define a local interface called
1
i f_i face. Notice that none of
the
interface com-
ponents have been defined within a visibility section. This is because all compo-
nents of an interface are implicitly defined within the public visibility section. If
you think about it, this makes sense because the purpose of an interface is to
expand the public interface of implementing classes.
INTERFACE 1i f_i face.
OATA: a TYPE string.
METHODS: m.
EVENTS: e.
ENOINTERFACE.
Listing 6.7 Syntax for Defining a Local Interface
Most of the time, you will just use interfaces to add additional methods to the
public interface of a class. However, you can technically define all of the same
types of components that you can define for classes in an interface (see Chapter 2,
Working with Objects, for more details on the types of components that you can
define for classes).
Interfaces can also be defined as global Repository objects in the Class Builder
tool. To illustrate how this works, let's create a new global interface called
ZI F_COMPARABLE that can be used to specify an ordering for implementing classes.
You will see an example of how to implement and use this interface in Sections
6.3.3, Implementing Interfaces, and 6.3.4, Working with Interfaces, respectively.
1. To create a global interface in the Class Builder, enter the name in the interface
in the OBJECT
TYPE
field, and click on the CREATE button (see Figure 6.2).
165