Skip to content

Inheritance Guide

Maurits edited this page Sep 10, 2017 · 2 revisions

Inheritance

This page will guide you through inheritance in SIMPLOO. The only thing you need is a basic understanding of Lua and object oriented programming.

Background

Inheritance can be described as a process in which one class acquires properties of another class.

For example; you can create a class called Animal with common functionality such as eat(), sleep() and move(). After that, you can create a new class called Dog and make this class inherit all properties from the Animal class. Your dog class is now also capable of eating, sleeping and moving, but nothing stops you from adding a bark() function to your dog.

Inheritance

SIMPLOO supports multiple inheritance. This means, that you can make one class extend upon the functionality of one or more other classes. Inheritance can be achieved using the extends keyword.

Here's a example where we inherit from one class:

class "Animal" {
    public {
        doWalk = function(self)
            print("Walking!")
        end;
        
        doEat = function(self)
            print("Eating!")
        end;
        
        doSleep = function(self)
            print("Sleeping!")
        end;
    };
}

class "Dog" extends "Animal" {
    public {
        doBark = function(self)
            print("Woof!")
        end;
    };
}

local dog = Dog.new();
dog:doWalk() -- prints 'Walking!'
dog:doEat() -- prints 'Eating!'
dog:doSleep() -- prints 'Sleeping!'
dog:doBark() -- prints 'Woof!'

SIMPLOO uses a registry-like system to find direct access to all members available in a class. This means that you can chain many classes together but still maintain a constant performance when accessing class members. However, the speed of instantiation will decrease as you chain more and more classes together, because the entire class inheritance tree has to be duplicated every time you create a new instance.

Here's an example where we inherit from two classes:

class "Scanner" {
    public {
        getScannedDocument = function(self)
            print("Beep beep we're scanning!")
            
            return "This string represents a document"
        end;
    };
}

class "Printer" {
    public {
        printDocument = function(self, document)
            print("Printing: " .. document)
        end;
    };
}

class "Copier" extends "Scanner, Printer" {
    public {
        makeCopy = function(self)
            local document = self:getScannedDocument()
            
            self:printDocument(document)
        end;
    };
}

local copyMachine = Copier.new()
copyMachine:makeCopy() -- 'Beep beep we're scanning!'   'Printing: This string represents a document'

When you decide to inherit from more than one class, things may get a bit more complicated. For example; if you were to inherit from two classes which both have a member function called 'getValue()', then calling that function on yourself would lead to ambiguities: which parent function should we execute? Fortunately you can work around this in SIMPLOO by explicitly specifying which parent function to call.

Here's an example of multiple inheritance where the ambiguity problem is solved:

class "TemperatureGauge" {
    public {
        getValue = function(self)
            return math.random(-20, 50) -- This is just an example value.
        end;
    };
}

class "HumidityGauge" {
    public {
        getValue = function(self)
            return math.random(0, 100) -- This is just an example value.
        end;
    };
}

class "WeatherMonitor" extends "TemperatureGauge, HumidityGauge" {
    public {
        displayOverview = function(self)
            print("Weather Overview:")
            print("- Temperature: " .. self.TemperatureGauge:getValue()) -- Here we explicitly say that we want to get the value of our TemperatureGauge parent.
            print("- Humidity: " .. self.HumidityGauge:getValue())
        end;
    };
};

local weatherMonitor = WeatherMonitor.new()
weatherMonitor:displayOverview()

When you extend from one or more classes, the names of these classes will become class members that can be used to explicitly access a parent. This means that you cannot define usable class members with the same name as a parent class.

Clone this wiki locally