OOP in more detail

Looks at subjects like inheritence

by f_3v

Author Avatar

Before you begin

I assume before you begin that you have already looked at the other OOP tutorial here, or at least have a

basic understanding of how it works. I'm going to go into more detail and more advanced OOP here so if you

haven't watched that don't rate low because it was too diffuclt.

Starting code

Here is a simple example of an object that I created with a few properties and methods

Animal = {}
Animal.__index = Car

function Animal.new(colour, name, weight)
        local newanimal = {}
    setmetatable(newanimal, Animal)

    -- I'm using the UK spelling of colour, if you are from the US feel free to use color for the code
    newanimal.Colour = colour
    newanimal.Name = name
    newanimal.Weight = weight

    return newanimal
end

function Animal:Eat()
   self.Weight = self.Weight + 1
end

Inheritance

Noticed how I haven't added a species property, I'll show you why now. If we want each species to have there own

properties that aren't a part of every animal like dog breeds, fur colours, wing width etc we can create a seperate

class for each of the animals and have it inherit the properties that all animals have from the animal class. If you

don't get it, let me show you. Here is a class for a dog animal:

-- We'll import our animal class that I showed you earlier
Animal = require(game.ReplicatedStorage.Animal)

-- Then we'll just continue like any other object for now
Dog = {}
Dog.__index = Dog

-- In order for a Dog object to be able to use all of the properties of the Animal class, we need to set the

-- metatable of dog to the Animal class

setmetatable(Dog, Animal)

-- For our constructor, we will add in all of the properties of the animal class in the same order first, and then
-- add properties that only belong to the dog class 
function Dog.new(colour, name, weight, breed)
    -- Here is where things change. We will set newdog to create a new instance of our animal class, and then set the
    -- metatable as normal
    local newdog = Animal.new(colour. name, weight)

    -- Now we just assign all of the properties that are exclusive to dog to the newdog instance
    newdog.breed = breed

    return newdog
end

return Dog

What we did here was create an animal class, and then create a dog class that inherits from the animal class. This dog

class will have all the properties of the animal and a few extras that are exclusive to dogs and that no other animals

have. You can use all of the methods that the Animal class has in a Dog object as well. I'll show you an example:

Dog = require(game.ReplicatedStorage.Dog)

james = Dog.new("Brown", "James", "10", "German Shepard")
print(james.Weight)
james:Eat()
print(james.Weight)

The code below would print 10 and then 11, because the Dog object gets all it's properties from the Animal class

You can also create a cat class, bird class, ect the same way we created our Dog class with different properties

and all of the objects would have everything Animal has with anything else you decide to add. If you understood and

learnt something, please leave a positive review. Also, feel free to message me on ROBLOX (f_3v) if you are having

trouble and I'll be happy to help. Thanks for reading!

View in-game to comment, award, and more!