Extending classes


It's common, when dealing with objects, to take an existing class and extend it. One way to do this is to modify the source code of the original class -- but this isn't always available, and with many different people modifying a class, classes could rapidly get over-complicated.

Languages that deal with objects, like NetRexx, therefore allow new classes of objects to be set up which are derived from existing classes. For example, if you wanted a different kind of Oblong in which the Oblong had a new property that would be used when printing the Oblong as a rectangle, you might define it thus:

  /* charOblong.nrx -- an oblong class with character */
  class charOblong extends Oblong
    printchar       -- the character for display

/* Constructor method to make a new oblong with character */ method charOblong(new_width, new_height, new_printchar) super(new_width, new_height) -- make an oblong printchar=new_printchar -- and set the print character

/* 'Print' the oblong */ method print loop for this.height say printchar.copies(this.width) end

There are several things worth noting about this example:

  1. The 'extends Oblong' on the class statement means that this class is an extension of the Oblong class. The properties and methods of the Oblong class are inherited by this class (that is, appear as though they were part of this class).

    Another common way of saying this is that 'charOblong' is a subclass of 'Oblong' (and 'Oblong' is the superclass of 'charOblong').

  2. This class adds the 'printchar' property to the properties already defined for Oblong.
  3. The constructor for this class takes a width and height (just like Oblong) and adds a third argument to specify a print character. It first invokes the constructor of its superclass (Oblong) to build an Oblong, and finally sets the printchar for the new object.
  4. The new charOblong object also prints differently, as a rectangle of characters, according to its dimension. The 'print' method (as it has the same name and arguments -- none -- as that of the superclass) replaces (overrides) the 'print' method of Oblong.
  5. The other methods of Oblong are not overridden, and therefore can be used on charOblong objects.

The charOblong.nrx file is compiled just like Oblong.nrx was, and should create a file called charOblong.class. Here's a program to try it out:

  /* trycharOblong.nrx -- try the charOblong class */

first=charOblong(5,3,'#') -- make an oblong first.print -- show it first.sizerelative(1,1).print -- enlarge it and print it again

second=charOblong(1,2,'*') -- make another oblong second.print -- and print it

This should create the two charOblong objects, and print them out in a simple 'character graphics' form. Note the use of the method 'sizerelative' from Oblong to resize the charOblong object.

Optional arguments

All methods in NetRexx may have optional arguments (omitted from the right) if desired. For an argument to be optional, you must supply a default value. For example, if the charOblong constructor was to have a default printchar value, its method statement could have been written:

  method charOblong(new_width, new_height, new_printchar='X')

which indicates that if no third argument is supplied then 'X' should be used. A program creating a charOblong could then simply write:

  first=charOblong(5,3)       -- make an oblong

which would have the same effect as if 'X' were specified as the third argument.


[ previous section | contents | next section ]

From 'nrover.doc', version 1.113.
Copyright(c) IBM Corporation, 1996, 1997. All rights reserved. ©