# Method: $inherit() - Object Inheritance

Updated: 11/30/2021, 7:00:45 PM
Created: 11/30/2021, 7:00:45 PM
Last Updated By: Jake Tuttle
Read Time: 1 minute(s)

Object inheritance is a mechanism that allows an instance of a class to inherit all of the properties and methods of the classes of other object instances. Object inheritance is recursive in the same way as Class inheritance.

An example will make this clearer:

Here we have 3 classes: Vehicle, Engine, Body:

ID: class_vehicle.jabba

method Vehicle::Vehicle()
    varg = new object("$vararg")
    try
        if varg->size() ne 1 then throw "Too many arguments, only 1 allowed!"
        this->type = varg->next()
        this->make = ""
        this->model = ""
    catch ex
        crt ex->message
        stop
    end try
end method

method Vehicle::model(modelname)
    this->model = modelname
    return this
end method

method Vehicle::make(make)
    this->make = make
    return this
end method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ID: class_engine.jabba

method Engine::Engine()
    this->cylinders = 6      ;* default
end method
1
2
3
4
5
ID: class_body.jabba

method Body::Body()
    this->color = "black"    ;* default
    this->size = "standard"  ;* default
end method

method Body::display()
    beautify = 5
    crt this->$tojson(beautify)
end method
1
2
3
4
5
6
7
8
9
10
11

We can now build new car object like this:

ID: car.jabba

car = new object("Vehicle", "car")
car->make("Jupiter Motors")->model("Europa")

body = new object("Body")

engine = new object("Engine")
engine->cylinders = 4

car->$inherit(body, engine)
car->display()
1
2
3
4
5
6
7
8
9
10
11
12

After assigning some initial properties to the car object we then inherit the properties and methods from the body and engine instances. If their classes had inherited from other classes then those inherited members would be available to our car object

Running this code produces the following output:

{
    "type":"car",
    "make":"Jupiter Motors",
    "model":"Europa",
    "color":"black",
    "size":"standard",
    "cylinders":6
}
1
2
3
4
5
6
7
8

Back to Inheritance