home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 6
/
FreshFish_September1994.bin
/
new
/
dev
/
obero
/
oberon-a
/
texts
/
oberon2.oop.text
< prev
next >
Wrap
Text File
|
1993-11-02
|
27KB
|
602 lines
OBJECT-ORIENTED PROGRAMMING IN OBERON-2
Hanspeter Mssenbck
ETH Z rich, Institut f r Computersysteme
ABSTRACT
Oberon-2 is a refined version of Oberon developed at ETH. It introduces
type-bound procedures, read-only export of data, and open array variables.
The For statement is reintroduced. This paper concentrates on type-bound
procedures which make Oberon-2 an object-oriented language with
dynamically-bound messages and strong type checking at compile time.
Messages can also be sent as data packets (extensible records) that are
passed to a handler procedure and are interpreted at run time. This is as
flexible as the Smalltalk message dispatching mechanism. Objects carry type
information at run time which allows dynamic binding of messages, run time
type tests, and the implementation of persistent objects. Oberon-2 is
available on various machines.
OVERVIEW
In 1987, Wirth defined the language Oberon [1]. Compared with its
predecessor Modula-2, Oberon is smaller and cleaner, and it supports type
extension which is a prerequisite for object-oriented programming. Type
extension allows the programmer to extend an existing record type by adding
new fields while preserving the compatibility between the old and the new
type. Operations on a type, however, have to be implemented as ordinary
procedures without syntactic relation to that type. They cannot be redefined
for an extended type. Therefore dynamically-bound messages (which are vital
for object-oriented programming) are not directly supported by Oberon,
although they can be implemented via message records (see below).
Compared to Oberon, Oberon-2 [2] provides type-bound procedures (methods),
read-only export of data, and open array variables. The For statement is
reintroduced after having been eliminated in the step from Modula-2 to
Oberon. This paper concentrates on type-bound procedures and the use of
Oberon-2 for object-oriented programming. The other facilities are described
in the Oberon-2 language report.
Type-bound procedures are operations applicable to variables of a record or
pointer type. They are syntactically associated with that type and can
therefore easily be identified as its operations. They can be redefined for
an extended type and are invoked using dynamic binding. Type-bound
procedures together with type extension make Oberon-2 a true object-oriented
language with dynamically-bound messages and strong type checking at compile
time. Oberon-2 is the result of three years experience of using Oberon and
its experimental offspring Object Oberon [3]. Object-oriented concepts were
integrated smoothly into Oberon without sacrificing the conceptual
simplicity of the language.
Object-oriented programming is based on three concepts: data abstraction,
type extension and dynamic binding of a message to the procedure that
implements it. All these concepts are supported by Oberon-2. We first
discuss type extension since this is perhaps the most important of the three
notions, and then turn to type-bound procedures, which allow data
abstraction and dynamic binding.
TYPE EXTENSION
Type extension was introduced by Wirth in Oberon. It allows the programmer
to derive a new type from an existing one by adding data fields to it.
Consider the declarations
TYPE
PointDesc = RECORD x, y: INTEGER END;
PointDesc3D = RECORD (PointDesc) z: INTEGER END;
Point = POINTER TO PointDesc;
Point3D = POINTER TO PointDesc3D;
PointXYZ = POINTER TO PointDescXYZ;
PointDescXYZ = RECORD x, y, z: INTEGER END;
PointDesc3D is an extension of PointDesc (specified by the type name in
parentheses that follows the symbol RECORD). It starts with the same fields
as PointDesc but contains an additional field z. Conversely, PointDesc is
called the base type of PointDesc3D. The notion of type extension also
applies to pointers. Point3D is an extension of Point and Point is the base
type of Point3D. Type extension is also called inheritance because one can
think of PointDesc3D as "inheriting" the fields x and y from PointDesc.
The crucial point about type extension is that Point3D is compatible with
Point, while PointXYZ is not (though it also points to a record with the
fields x and y). If p is of type Point and p3 is of type Point3D the
assignment
p := p3
is legal since p3 is an (extended) Point and therefore assignment compatible
with p, which is a Point. The reverse assignment p3 := p is illegal since p
is only a Point but not a Point3D like p3. The same compatibility rules
apply to records.
Objects which are pointers or records have both a static type and a dynamic
type. The static type is the type which the object is declared of. The
dynamic type is the type which the object has at run time. It may be an
extension of its static type. After the assignment p := p3 the dynamic type
of p is Point3D, while its static type is still Point. That means that the
field p3^.z is still part of the block that p points to, but it cannot be
accessed via p since the static type of p does not contain a field p^.z
(Figure 1).
Figure 1. Assignment between the extended object and the base object
Objects like p are polymorphic, i.e. they may assume various types at run
time. The actual type an object has at run time can be examined with a type
test:
p IS Point3D
yields TRUE if the dynamic type of p is Point3D (or an extension of it) and
FALSE otherwise. A type guard
p(Point3D)
asserts (i.e., tests at run time) that the dynamic type of p is Point3D (or
an extension of it). If so, the designator p(Point3D) is regarded as having
the static type Point3D. If not, the program is aborted. Type guards allow
the treatment of p as a Point3D object. Therefore the following assignments
are possible: p(Point3D)^.z := 0; p3 := p(Point3D);
For objects of a record type, the static and the dynamic types are usually
the same. If pd is of type PointDesc and pd3 is of type PointDesc3D, the
assignment pd := pd3 does not change the dynamic type of pd. Only the fields
pd3.x and pd3.y are moved to pd, and the dynamic type of pd remains
PointDesc. The compatibility between records is of minor importance except
when pd is a formal variable parameter and pd3 is its corresponding actual
parameter. In this case the dynamic type of pd is Point3D and the component
pd3^.z is not stripped off.
The motivation for type extension is that an algorithm which works with type
Point can also work with any of its extensions. For example, the procedure
PROCEDURE Move (p: Point; dx, dy: INTEGER);
BEGIN INC(p.x, dx); INC(p.y, dy)
END Move;
can be called not only as Move(p, dx, dy) but also as Move(p3, dx, dy).
TYPE-BOUND PROCEDURES
Type-bound procedures serve to implement abstract data types with
dynamically bound operations. An abstract data type is a user-defined type
which encapsulates private data together with a set of operations that can
be used to manipulate this data. In Modula-2 or in Oberon an abstract data
type is implemented as a record type and a set of procedures. The
procedures, however, are syntactically unrelated to the record, which
sometimes makes it hard to identify the data and the operations as an
entity.
In Oberon-2, procedures can be connected to a data type explicitly. Such
procedures are called type-bound. The interface of an abstract data type for
texts may look like this:
TYPE
Text = POINTER TO TextDesc;
TextDesc = RECORD
data: ... (*(hidden) text data*)
PROCEDURE (t: Text) Insert (string: ARRAY OF CHAR; pos: LONGINT);
PROCEDURE (t: Text) Delete (from, to: LONGINT);
PROCEDURE (t: Text) Length (): LONGINT;
END;
This gives a nice overview showing which operations can be applied to
variables of type Text. However, it would be unwise to implement the
operations directly within the record since that would clutter up the
declarations with code. In fact, the above view of Text was extracted from
the source code with a browser tool. The actual Oberon-2 program looks like
this:
TYPE
Text = POINTER TO TextDesc;
TextDesc = RECORD
data: (*(hidden) text data*)
END;
PROCEDURE (t: Text) Insert (string: ARRAY OF CHAR; pos: L