A new useful way which can make you code better in Luau OOPs

If you are a programmer or a developer who has past experience in programming in other programming languages except Luau, such as Java, C++, and C#, you may notice that all three languages are OOP languages. And in Luau, you can also do so.
But have you ever wondered how to extend the classes?
For example, you have a ModuleScript called tool, which would be used in OOP, and you also have a ModuleScript called knife or gun, etc. And knife or gun is in a category of tool.
So if you were programming in Java, you would do like public class knife extends tool {...} or if you were using C#, you would do public class knife : tool {...},
So, how would you do this in Luau, there’s no keyword like extend from Java, or : syntax from C#.
The Simple solution is in the knife class, require the ModuleScript or Class for eg in our case tool, in a local variable like Tool, then just like we do in the knife.new Function, we call setmetatable({}, knife), “knife Here is The ModuleScript/Class/Table Name In That ModuleScript”, We call setmetatable function outside knife.new function like this setmetatable(knife, Tool) “Here knife is the table inside the ModuleScript and Tool is The Class, knife is extending”.


Here’s The Sample Code For You To Visualize it Easily:
Tool Module Script:

local Tool = {}
Tool.__index = Tool

function Tool:Use()
	--Code To Use It...
end

function Tool.new()
	local tool = {}
	setmetatable(tool, Tool)
	return tool
end

return Tool

Knife Module Script:

local Tool = require(script.Parent.Tool)
local Knife = {}
Knife.__index = Knife
setmetatable(Knife, Tool)

function Knife:Backstab()
	--Code to backstab someone, maybe your friend?
end

function Knife.new()
	local knife = {}
	setmetatable(knife, Knife)
	return knife
end

return Knife

There are way more things in Subtopic of OOP, which is “Inheritance”,
The thing I told you are the part of inheritance,
In this you can also override methods like in Java we do:

@Override
public static void example(){
}

In Luau we would do in the above taught context,
we do

function Knife:Use()
	--Overrided method
end

(Make Sure That The Method name and the method parameters are same as the the method you are overriding otherwise it would create another method!)
And you may wonder now, i have taught that much, I didnt told how to call the super method (orignal Method in The great-grandparent class the children is extending)
So we do this by:

function Knife:Use()
	--Overrided method
    Tool:Use()
end

Note that Luau Is a loosely typed language and it doesnt have much control over data and variable so theres no defined way to create things like interfaces like in Java or C#,
Last Tip Of The Teacher:
To Create An interface, Just Do What I Told, Make A Class Which other class would be extending, just create methods, leave the methods empty.

Thanks, Thats all from my side, Thank you,
If you have any suggesstions or correction in error in my this topic, kindly correct it by sending a reply by quoting the error.

6 Likes