Inheritance
Like all object-oriented programming languages, Lua++ supports full class inheritance. To inherit all the heritable properties of a class in Lua++, one would separate the class name from the base class using a colon in this way: tag : base-class
, and for multiple class inheritance, one would separate each base class with a comma: tag: base-1, base-2, ...
as shown in the following example:
Here we define a base class Person
, with the firstName
and lastName
properties. This class has two public methods, getName()
, and getDescription()
. As mentioned earlier, to inherit a class you use the :
operator. For example, the following Employee
class inherits properties and methods from the Person
class:
Since the Person
class has a constructor that initializes the firstName
and lastName
properties, you need to initialize these properties in the constructor of the Employee
class by calling its parent class’ constructor. This can be done by using the base()
keyword.
The following creates an instance of the Employee
class which inherits all the methods and properties of the Employee
class:
Lua++ also allows you to override methods inherited by a base class. This can be done by using the :
symbol after the name definition of the class.
The output of this fragment of code will be:
Last updated