Object-oriented features
- Class: Class is an extensible template used to create objects, providing the initial value of state (member variables) and implementation of behavior.
- Object: It is an instance of the class and has independent memory allocated to itself.
- Inheritance: It is the concept that variables and functions of classes are inherited by other classes.
- Encapsulation: It is a method within a class that combines data and functions. Data can be accessed with the help of external classes and functions. It is also called data abstraction.
Lua's OOP
Implement the first type of function that is object-oriented and table-oriented and Lua in Lua. An object is formed by inserting functions and related data into a table. Inheritance can be implemented with the help of metatables, providing a lookup mechanism that does not exist and in the parent object field.
There are such states and identification objects in the Lua table, which are independent of the value. Two objects (tables) have the same value but in different objects, while one object can have the same value but it is always the same object. Just like there is a life cycle in an object table, created independently or being created.
A real world example
The concept of object-oriented is broad, but you need to understand and obtain the best interests.
Let's consider a simple mathematical example. We often encounter situations where we work in different shapes like circles, rectangles and squares.
Shapes can have a common attribute area. Therefore, we can extend other shapes from the base object shape with the common attribute area. Each shape can have its own properties and functions. Rectangles can have properties of length, width, area as their properties, printArea and calculateArea as its functions.
Create a simple class
A simple class implements the three attribute area of the rectangle, the length and width as shown below. It also has a printArea function to print the calculated area.
Rectangle = {area = 0, length = 0, breadth = 0}
-- Derived class method new
function Rectangle:new (o,length,breadth)
o = o or {}
setmetatable(o, self)
self.__index = self
= length or 0
= breadth or 0
= length*breadth;
return o
end
-- Derived class method printArea
function Rectangle:printArea ()
print("The area of Rectangle is ",)
end
Create an object
Creating an object is the process of allocating memory for instances of a class. Each object has its own memory and shared public class data.
Access properties
Use the dot . operator in the class, as shown in the figure below, and you can access the properties
Access member functions
Use the colon operator as shown in the figure below to access object member functions.
The memory is allocated and the initial value is set. The initialization process can be constructed in other object-oriented languages. It is just a function setting, as shown in the figure above.
Complete example
Let's take a look at a complete example of using object-oriented Lua.
Shape = {area = 0}
-- Base class method new
function Shape:new (o,side)
o = o or {}
setmetatable(o, self)
self.__index = self
side = side or 0
= side*side;
return o
end
-- Base class method printArea
function Shape:printArea ()
print("The area is ",)
end
-- Creating an object
myshape = Shape:new(nil,10)
myshape:printArea()
When running the above program, you will get the following output.
Lua's inheritance
Inheritance is the processing of extending basic objects with simple shapes to rectangles, squares, etc. It is often used for the basic nature and functionality of sharing and extending in the real world.
Let's look at a simple class extension. There is a class as shown in the figure below.
Shape = {area = 0}
-- Base class method new
function Shape:new (o,side)
o = o or {}
setmetatable(o, self)
self.__index = self
side = side or 0
= side*side;
return o
end
-- Base class method printArea
function Shape:printArea ()
print("The area is ",)
end
The shape we can expand is the square class as shown below.
-- Derived class method new
function Square:new (o,side)
o = o or Shape:new(o,side)
setmetatable(o, self)
self.__index = self
return o
end
Overload basic functions
We can overload the base class function using the functions in the base class, instead of derive the class and then implement it itself, as shown in the figure below
function Square:printArea ()
print("The area of square is ",)
end
Complete example of inheritance
A simple class implementation that we can extend in Lua, as shown in the figure above to metatables another new method. All member variables and base class functions are retained in the derived class.
Shape = {area = 0}
-- Base class method new
function Shape:new (o,side)
o = o or {}
setmetatable(o, self)
self.__index = self
side = side or 0
= side*side;
return o
end
-- Base class method printArea
function Shape:printArea ()
print("The area is ",)
end
-- Creating an object
myshape = Shape:new(nil,10)
myshape:printArea()
Square = Shape:new()
-- Derived class method new
function Square:new (o,side)
o = o or Shape:new(o,side)
setmetatable(o, self)
self.__index = self
return o
end
-- Derived class method printArea
function Square:printArea ()
print("The area of square is ",)
end
-- Creating an object
mysquare = Square:new(nil,10)
mysquare:printArea()
Rectangle = Shape:new()
-- Derived class method new
function Rectangle:new (o,length,breadth)
o = o or Shape:new(o)
setmetatable(o, self)
self.__index = self
= length * breadth
return o
end
-- Derived class method printArea
function Rectangle:printArea ()
print("The area of Rectangle is ",)
end
-- Creating an object
myrectangle = Rectangle:new(nil,10,20)
myrectangle:printArea()
When we run the above program, we will get the following output.
The area of square is 100
The area of Rectangle is 200
In the example above, we created two derived classes Rectangle and Square from the base class Square. Therefore, the derived classes of the functions of the base class can be changed here. In this implementation example, the derived class replaces the function printArea.