Skip to content

Inheritance

Richard Baltrusch edited this page Mar 18, 2022 · 3 revisions

Inheritance

Inheritance in objectbatch is implemented using the super keyword, which uses the __super__ variable. The latter typically is defined at the top of a class definition along with the class header. If a __super__ variable is not defined for a particular class, it automatically inherits from object.

The following class header defines the superclass myClass2:

set __super__=myClass2
call class %*
%class%

Note that a call to super, which is used in methods, such as the constructor or other inherited methods, "consumes" the __super__ variable by setting it to zero. This has the effect that combining method calls to multiple inherited methods is not possible in a single method.

Inheriting attributes

Attributes may be inherited from the defined superclass with a super call in the constructor. Note that the constructor should start with a super call whether a superclass is defined or not, as classes that don't define a superclass inherit the base object constructor, which is required to instantiate a new object.

A normal constructor would then inherit all attributes from all its superclasses using the following syntax:

:public-construct
	call super %*
exit /b

Inheriting methods

Public methods from the parent class (i.e. the direct superclass) may be inherited using the super keyword. For example, to inherit the method myMethod defined in the parent class, we may define:

:public-myMethod
    call super %*
exit /b

We may also use super in this way to modify the functionality of the inherited function by adding code before or after the super call.

Multilevel inheritance

Multilevel inheritance, by which a class may inherit from a derived class (a class which itself inherits from another class), is possible in objectbatch. For example, we may define the following class headers for classes MyClass and MyClass2 (these headers should be defined in separate class definition files):

::class header for MyClass.bat
set __super__=myClass2
call class %*
%class%
::class header for MyClass2.bat
call class %*
%class%

In the example above, we have the following multilevel inheritance chain:

myClass --> myClass2 --> object *
  • --> means "inherits from".

Multiple inheritance

Multiple inheritance, by which a class may inherit attributes and methods from more than one parent class, is not supported by objectbatch.

More information

More information can be found in the home page, the quick start page and the examples.

Clone this wiki locally