A class can be derived from one or more superclasses (a superclass is a more general class), the operator inherit is used to declare superclasses of a class. Below there is an example of this:
| class base. endclass base. class derived. inherit base. %The 'derived' class is derived from 'base' endclass derived. |
When a class name is used in the inherit section of a declaration it must have been declared before the line containing the inherit operator. The public methods of the super classes are accessible, the private methods of the super classes are not accessible. Private methods can only be called by other methods of the same class, not by any subclasses.
| class base. public. do_something/1. private. calculate/2. endclass base. class derived. inherit base. %The 'derived' class is derived from 'base' public. test/1. endclass derived. |
The class 'base' in the example above has a private method called 'calculate/2', this method can be called by 'do_something/1' method of the same class but by any other methods of the class 'derived'. The method 'do_something/1' is a public method so it can be called by any method or predicate which has access to the public methods of the class 'derived'.
Multiple inheritance is possible, all the names of the base classes must be separated by a comma. Below there is an instance of a multiple inheritance declaration.
| inherit base, base1, base2. |
| Because attributes must have unique names it is not allowed that a base class has an attribute with the same name as an attribute of a subclass! A compile error is generated if a duplicate attribute name is encountered in a hierarchy of classes. |
It is possible to use an undeclared class as a superclass for another class but the operator declareclass/1 must be used for that. With declareclass/1 only a class name is declared. The class declaration must follow in the remainder of the same file. Below there is an example of using declareclass/1.
| %the class 'base1' has not been declared declareclass base1. %the name 'base1' is now known class derived1. |