Cind programming language

Language definition Examples Download Contact

Class inheritance

Sometimes an object is just a special case of a more general object or it is slightly different from some other object.
In this case, it would be pointless to duplicate the class code of such an object.
One technique that can be used to avoid this is the so-called class inheritance.

In the inheritance relation a class can be connected to the other class using extends keyword in the class header, or an object of class can be connected to the other object by using over keyword in the header of its class.
For example:

class a { .f() { host.println("Hello from a!"); } } class b extends a { .g() { host.println("Hello from b!"); } }

is a definition of two classes a and b. Class b extends from the class a, which is declared in the header of class b
(for more about classes see Classes).

The essence of the inheritance relation is, that object of the above class b can be threated as a cover for the object of the class a. In the sense, that any not catched message, operator or type conversion call, by the first object, will be passed to the other object.
For example, in the code:

var x = new b(); x.f();

there is created a new object of class b and a f message is sended to it. Since there is no method f in the above class b, then this message will be passed in the inheritance relation, to the object of class a, and catched there.

Notice, that the second object created by inheritance relation is assigned to the first object, but it is created in lazy way, which means that it will not be created at the time of creation of the first object, but will be created the first time when it is needed (perhaps never).

When it is not specified which object to inherit from (no extends nor over declaration in the class), then object of object class will be created instead (see Objects and null value).

Inheritance chain

The first object inherits from the second object. The second object can also inherits from the third object, and so on. In this way we will get an inheritance chain of objects, linked by inheritance relation.

There are special indicator objects, that can be accessed inside the code of given class, to handle objects of inheritance chain directly, these are: this, under and above. Indicator this points to the object of given class, indicator under points to the next object in chain, and indicator above points to the object that is on the top of the chain (see Indicator objects).

Direct indication

The another way of linking objects in inheritance chain is a direct indication of the object after which will inherit from, using keyword over in the header of class declaration.
For example:

class c over x { ... }

it defines class c with indication that object x will be connected as the second object in the inheritance chain for every object of class c. It doesn't matter of what class is the object x or whether it has a class at all.

Chains and cycles

Because the direct indication can make inheritance chains of miscellaneous objects, then some objects may exists in more then one chain and also inheritance chain may contains cycles.

Notice also, that in chains which are linked by direct indication, pointer above may not points to the first object of expected chain.

Initialization of the under object

Since objects in inheritance chain are created in lazy way, it is possible to pass parameters to the constructor of next object before it will be created. There is special syntax to do that: writing vector of parameters after keyword under will save this vector to be passed to the constructor of object under when it will be created.
Example:

under(a,b,c);

The result of that statement is true when vector is saved to be used or false when object under is already created.



Cind programming language 1.0.4