PreviousClass Programs Requirements-based VocabularyNext

Chapter 12: Extending a Class

This chapter shows you how you can use a class extension to add extra functionality to a class without changing its source code. Class extension is a Micro Focus feature of Object COBOL.

12.1 Overview

A class extension enables you to add new methods to an Object COBOL class without changing the original source code. The difference between extending a class and subclassing it is that the extensions are inherited by all existing subclasses.

For example, if class A has a subclass, class B, you can add functionality to class A by subclassing it to create subclass C. However, class B will not inherit the functionality of class C, because it is a subclass of A. If you extend class A with a class extension, X, the effect at run-time is the same as if you had changed and recompiled class A. Class B inherits all the extra functionality in class X.

12.2 Extending a Class

To extend a class, write a class program where the class header looks like this:

 class-id. ExtensionName extend OriginalClass [with data].

The Class-Control paragraph must contain references to the extension class and the class which it extends. Use the WITH DATA clause to enable the extension class to reference class and instance data of the original class directly. The extension class cannot declare new class or instance data of its own, although it can declare its own Working-Storage.

To use the class extension, you must insert a COBOL CALL to it at any point in application execution before the class extensions are used, to register the new methods with the run-time system.

12.2.1 Example

The three short classes below show how class extension works. Class A is a subclass of Base and class B is a subclass of A. Neither of these classes have any methods. Class Axtnd is a class extension for A.

Class A:

 class-id. a inherits from base.

 class-control.
     a is class "a"
     base is class "base"
     .
 class-object.
 end class-object.

 object.
 end object.

 end class a.

Class B:

 class-id. b inherits from a.

 class-control.
     b is class "b"
     a is class "a"
     .
 class-object.
 end class-object.

 object.
 end object.

 end class b.

Class Axtnd (the extension of class A) implements a single instance method:

 class-id. axtnd extend a.

 class-control.
     axtnd is class "axtnd"
     a is class "a"
     .

 object.
 method-id. "extendMethod".
 procedure division.
     display "I'm an extension"
     exit method.
 end method "extendMethod".
 end object.

 end class axtnd.

The next piece of code is a short test which shows how the extension works:

 program-id. ExtensionTest.

 class-control.
     a is class "a"
     b is class "b"
     .
 working-storage section.
 01 thisB                object reference.
 procedure division.
     call "axtnd"          *> Load class extension
     invoke b "new" 
           returning thisB *> Create instance of B
     invoke thisB "extendmethod"                           *> Send a message
     stop run.

When you run the ExtensionTest program, it loads class Axtnd. All methods implemented in this class are now accessible as though they were methods of class A. When the test program sends the "extendMethod" message to an instance of class B, the extension method is executed, as though it were implemented in class A and inherited by class B.


Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.
PreviousClass Programs Requirements-based VocabularyNext