Lua Object Oriented Programming

In this article, we will explore the basics of Lua Object Oriented Programming, including classes, objects, inheritance, and more.

Lua Object oriented programming (OOP) is a programming paradigm that is widely used in modern programming languages. Lua, being a powerful and flexible scripting language, also supports OOP concepts.

It does not have built-in support for OOP concepts, but it provides a rich set of features that can be used to implement OOP. Lua uses tables as its main data structure, and tables can be used to implement objects and classes.



Lua Object Oriented Programming Classes

Classes are tables with methods and properties.

The methods are functions that operate on the table properties.

Properties store the object’s state.

In Lua, you can create classes using tables and metatables.

-- Define the class
MyClass = {}

-- Define the constructor
function MyClass:new(...properties)
... code
end

-- Class methods
function MyClass:methodName()
... code
end

Lua Object Oriented Programming Objects

Objects are instances of classes with their own unique state and behavior.

Lua creates objects using tables and metatables.

-- Instantiate an object
local MyObject = MyClass(...properties)

-- Access property of an object
MyObject.propertyName

-- Accessing method of the class
MyObject:methodName()

Let’s look at an example that illustrates the creation of a class and instantiation of its objects:

Example: 

-- Define a class named "Person" Person = {}-- Define a constructor method for the class function Person:new(name, age) local obj = {name = name, age = age} setmetatable(obj, self) self.__index = self return obj end-- Define a method for the class function Person:introduce() print("Hi, my name is " .. self.name .. " and I am " .. self.age .. " years old.") end-- Create a new object of the Person class local john = Person:new("John", 30)-- Call the introduce method of the john object john:introduce() --> Hi, my name is John and I am 30 years old.

Example Explanation

In the example above, we defined a class named “Person” and a constructor method named “new“.

The “new” method creates a new instance of the class and initializes its properties by creating a new object with these properties.

It then sets the metatable of the new object to the Person class and returns the object.

We also defined a method named “introduce“, which prints out a message about the person.

Finally, we created an instance of the Person class named “john” and called its “introduce” method.


Lua Object Oriented Programming Inheritance

Inheritance is the ability of a class to inherit the properties and methods of another class.

In Lua, we can implement inheritance by creating a new class that inherits from an existing class.

By setting the metatable of a table to another table, you can implement inheritance.

Let’s implement inheritance in Lua through an example:

Example: 

-- Define a class named "Person" Person = {}-- Define a constructor method for the class function Person:new(name, age) local obj = {name = name, age = age} setmetatable(obj, self) self.__index = self return obj end-- Define a method for the class function Person:introduce() print("Hi, my name is " .. self.name .. " and I am " .. self.age .. " years old.") end-- Define a class named "Employee" Employee = {}-- Define a constructor method for the class function Employee:new(name, age, salary) local obj = Person:new(name, age) obj.salary = salary setmetatable(obj, self) self.__index = self return obj end-- Define a method for the class function Employee:introduce() Person.introduce(self) print("I make " .. self.salary .. " dollars a year.") end-- Create a new object of the Employee class local jane = Employee:new("Jane", 25, 50000)-- Call the introduce method of the jane object jane:introduce() --> Hi, my name is Jane and I am 25 years old. I make 50000 dollars a year.

Example Explanation

Above code defines two classes, “Person” and “Employee“, in Lua. The “Person” class has a constructor and an “introduce” method to print out the person’s name and age.
The “Employee” class is a subclass of “Person” and has an additional “salary” property. It has its own constructor and “introduce” method to print out the employee’s salary as well as their name and age.
An instance of “Employee” named “jane” is created and the “introduce” method of the object is called, which prints out Jane’s name, age, and salary.

Lua Object Oriented Programming Encapsulation

Encapsulation involves hiding internal details from the outside.

The goal of encapsulation is to provide access to properties and methods through an interface while hiding implementation details.

Below example demonstrates the concept of encapsulation in Lua:

Example: 

-- Define a class named "Person" Person = {}-- Define a constructor method for the class function Person:new(name, age) local obj = {name = name, age = age} setmetatable(obj, self) self.__index = self return obj end-- Define getter and setter methods for the name property function Person:getName() return self.name endfunction Person:setName(name) self.name = name end-- Create a new object of the Person class local john = Person:new("John", 30)-- Get the name of the john object using the getName method print(john:getName()) --> John-- Set the name of the john object using the setName method john:setName("Johnny")-- Get the name of the john object again using the getName method print(john:getName()) --> Johnny

Above code defines a “Person” class with a constructor method that creates a new object with “name” and “age” properties. The class also has getter and setter methods for the “name” property. An object of the “Person” class named “john” is created, and the getter and setter methods are used to retrieve and set the “name” property of the object. The program outputs the initial and modified name values of the “john” object.


Lua Object Oriented Programming Polymorphism

In simple terms, polymorphism means “many forms“.

It is the ability of objects to take on many different forms or behave in different ways depending on the context in which they are used.

It allows for the creation of more generic and flexible code that can handle different types of objects without needing to know their specific implementation details.

Following example focuses on polymorphism in Lua:

Example: 

-- Define a class named "Vehicle" Vehicle = {}-- Define a method for the class function Vehicle:drive() print("Driving a generic vehicle") end-- Define a subclass named "Car" Car = {} setmetatable(Car, { __index = Vehicle })-- Define a method for the subclass function Car:drive() print("Driving a car") end-- Define another subclass named "Motorcycle" Motorcycle = {} setmetatable(Motorcycle, { __index = Vehicle })-- Define a method for the subclass function Motorcycle:drive() print("Riding a motorcycle") end-- Create objects of the classes local vehicle = Vehicle local car = Car local motorcycle = Motorcycle-- Call the drive method of the objects vehicle:drive() --> Driving a generic vehicle car:drive() --> Driving a car motorcycle:drive() --> Riding a motorcycle
This code defines a class named “Vehicle” and a method for the class. It then defines two subclasses named “Car” and “Motorcycle“. Each subclass has a method for driving a car or riding a motorcycle, respectively.
The subclasses inherit from the “Vehicle” class using metatables. Objects are created for each class, and the “drive” method is called for each object, which prints the appropriate message for each type of vehicle.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
1
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *