Updated April 19, 2023
Introduction to Lua Classes
An extensible template for object creation that provides the initial values for implementation of behavior and state or member variables are called classes in Lua and an instance of the class which occupies a separate memory location for itself is called an object of the class and a class in Lua offers inheritance and encapsulation properties where inheritance is a concept in which one class inherits the functions and variables of another class and encapsulation is when we combine the data and functions together inside a class and accessing the data from outside the class is called abstraction of data.
The syntax to define classes in Lua is as follows:
class_name = {member variables}
function class_name:new(member variables)
function_definition
end
function class_name:method_name()
method_definition
end
where class_name is the name of the class and member variables are parameters passed to the class, function is the keyword to define the function of the derived class and new is the keyword to create a derived class,
method_name is the name of the method defined in the class.
How Lua Classes Work?
Working of classes in Lua is as follows:
- An extensible template for object creation that provides the initial values for implementation of behavior and state or member variables are called classes in Lua.
- The initial values of the member variables is provided by the class for the implementation of the behaviors.
- An instance of the class which occupies a separate memory location for itself is called an object of the class.
- A class in Lua offers properties like inheritance and encapsulation.
- Inheritance is a concept in which one class inherits the functions and variables of another class.
- The class which inherits the functions and variables of another class is called derived class.
- The class from which the functions and variables of another class are inherited is called meta class.
- Encapsulation is when we combine the data and functions together inside a class and accessing the data from outside the class is called abstraction of data.
Examples
Lets us discuss examples of Lua Classes.
Example #1
Lua program to demonstrate classes and to create derived classes consisting of member variables and methods using which we compute the area of a circle and display the result as the output on the screen:
–defining a meta class by passing the member variables area and radius to compute the area of a circle
Circle = {area = 0, radius = 0}
— defining a derived class of Circle and computing the area of a circle
function Circle:new (obj,radius)
obj = obj or {}
setmetatable(obj, self)
self.__index = self
self.radius = radius or 0
self.area = radius*radius;
return obj
end
— defining the method of the derived class to compute the area of a circle and displaying it as the output on the screen
function Circle:displayArea ()
print("The area of the circle is ",self.area)
end
–creating an object of Circle class to pass the value to the member variables and then displaying the result
result = Circle:new(nil,3)
result:displayArea()
The output of the above program is as shown in the snapshot below:
In the above program, we are defining a meta class that accepts the member variables or state variables to compute the area of a circle. Then we are defining the derived class by using the keyword new that actually contains the formula to compute the area of a circle. Then we are defining a method of the derived class to print the computed area of the circle as the output on the screen. Then we are defining the objects of the class by using a new keyword to access the member variables and methods of the class to display the computed area of the circle as the output on the screen. The output is shown in the snapshot above.
Example #2
Lua program to demonstrate classes and to create derived classes consisting of member variables and methods using which we compute the area of a circle and display the result as the output on the screen:
–defining a mets class by passing the member variables area and side to compute the area of a square
square = {area = 0, side = 0}
— defining a derived class of square and computing the area of a square
function square:new (obj,side)
obj = obj or {}
setmetatable(obj, self)
self.__index = self
self.side = side or 0
self.area = side*side;
return obj
end
— defining the method of the derived class to compute the area of a square and displaying it as the output on the screen
function square:displayArea ()
print("The area of the square is",self.area)
end
–creating an object of square class to pass the value to the member variables and then displaying the result
result = square:new(nil,4)
result:displayArea()
The output of the above program is as shown in the snapshot below:
In the above program, we are defining a meta class that accepts the member variables or state variables to compute the area of a square. Then we are defining the derived class by using the keyword new that actually contains the formula to compute the area of a square. Then we are defining a method of the derived class to print the computed area of the square as the output on the screen. Then we are defining the objects of the class by using a new keyword to access the member variables and methods of the class to display the computed area of the square as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of classes in Lua through definition, syntax, and working of classes along with the properties offered by the classes in Lua with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “Lua Classes ” was beneficial to you. You can view EDUCBA’s recommended articles for more information.