Contents | Prev | Next | Index
Type-bound procedures are Oberon-2's approach to introducing the concept of methods for classes in this programming language. They are globally declared procedures which are to be associated with a record type declared in the same module. The binding is expressed by the type of a receiver parameter in the procedure heading. The receiver may be either a variable parameter of record type T or a value parameter of type pointer to record type T. The procedure is bound to the type and is considered to be local to it. It is a member of T. The receiver can only be specified immediately after the PROCEDURE keyword.
receiver ::= ( VAR <identifier> : <identifier> ) ::= ( <identifier> : <identifier> ) |
This compiler always maps type-bound procedures to non-static Java methods unless a JAVA STATIC directive indicates that it is to be mapped to a static Java method.
If a procedure P is bound to a type T0, it is automatically also bound to any type T1 that is an extension of T0. However, a procedure P' (with the same name as P) may be explicitly bound to T1, in which case it overrides the binding of P. P' is considered to be a redefinition of P for T1. The formal parameters of P and P' must match. If P and T1 are exported, P' must be exported, too.
Java allows method overloading, that is, using the same method name with different formal parameter lists. This is not allowed in Oberon-2. This compiler, however, supports the calling of an overloaded method for a directly imported foreign Java class. Overriding inherited overloaded methods is also possible with this compiler by using different method names. These methods must have an additional JAVA NAME directive pointing to the same original Java method name.
If v is a designator and P is a type-bound procedure (method), then v.P denotes that procedure P that is bound to the dynamic type of v. This is known as dynamic binding in Oberon-2 terminology. It is known as virtual methods in Java. Note that this may be a different method procedure than the one bound to the static type of v. v is passed to P's receiver according to the parameter passing rules specified in the section about formal parameters. If P is declared as a type-bound procedure and also has a leading JAVA STATIC directive in its procedure declaration, then v.P denotes the procedure that is bound to the static type of v. This is called static binding. In Java it is known as static methods or class methods. Standard Oberon-2 does not include the concept of class methods. The only reason for this compiler to support above described limited concept of class methods is because of Java. Something similar can also be accomplished by declaring regular procedures in the same module where the type-bound procedures (virtual methods) for a record type (class) were introduced, along with global private variables.
If r is a receiver parameter declared with type T, r.P^ denotes the (redefined) procedure P bound to the base type (parent class) of T.
In a forward declaration of a type-bound procedure, the receiver parameter must be of the same type as in the actual procedure declaration. The formal parameter lists of both declarations must match.
Examples:
PROCEDURE( t : Tree ) Insert( node : Tree );
VAR p, father : Tree;
BEGIN
p := t;
REPEAT
father := p;
IF node.key = p.key THEN RETURN END;
IF node.key < p.key THEN p := p.left ELSE p := p.right END
UNTIL p = NIL;
IF node.key < father.key THEN father.left := node
ELSE father.right := node
END;
node.left := NIL; node.right := NIL
END Insert;PROCEDURE( t : CenterTree) Insert( node : Tree ); (*redefinition*)
BEGIN
WriteInt( node(CenterTree).width );
t.Insert^(node) (*calls the Insert procedure bound to Tree *)
END Insert;
Contents | Prev | Next | Index
Canterbury Oberon-2 for JVM (Last documentation update
Jun 2, 2000)
Copyright © 1998 Mill Hill &
Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to
mhc@webcom.com