Lua Object Oriented - Lua (programming language)

What is the use of OOP in Lua?

Introduction to OOP

Object Oriented Programming (OOP) is one the mainly used programming technique in the modern era of programming. There are many programming languages supported OOP which include:

  • C++
  • Java
  • Objective-C
  • Smalltalk
  • C#
  • Ruby

Features of OOP

  • Class − Class is an extensible template to create objects, provide initial values for state (member variables) and implementations of behavior.
  • Objects − Object is an instance of class and it has separate memory allocated for itself.
  • Inheritance − Inheritance is a concept by which variables and functions of one class are inherited by another class.
  • Encapsulation − Encapsulation is the process of combining data and functions inside a class. Data can be accessed outside the class with the help of functions. It is also known as data abstraction.

OOP in Lua

Object orientation can be implemented in Lua with the help of tables and first class functions of Lua. An object will be formed by placing functions and related data into a table. Inheritance is implemented with the help of metatables, providing a look up mechanism for nonexistent functions (methods) and fields in parent object(s).

Tables in Lua have the features of object like state and identity which is independent of its values. Two objects (tables) with same value will be different objects, but an object can have different values at different times, but it is always the same object. Like objects, tables will have a life cycle which is independent of who created them or where they were created.

A Real World Example

Concept of object orientation is broadly used but you should understand it clearly to get benefited.

Let’s consider a simple math example. We frequently encounter situations where we work on different shapes like circle, rectangle and square.

Shapes will have a common property Area. Therefore, other shapes can be extended from the base object shape with common property area. Each of the shapes will have its own properties and functions like a rectangle can have properties length, breadth, area as its properties and printArea and calculateArea as its functions.

Creating a Simple Class

Below is a simple class implementation for a rectangle with three properties area, length, and breadth. It also has a printArea function to print the area calculated.

Creating an Object

Creating an object is the process of allocating memory for the class instance. Each of the objects will have its own memory and shares the common class data.

Accessing Properties

Properties in a class can be accessed using the dot operator as shown below

Accessing Member Function

Member function can be accessed using colon operator with the object as shown below

Memory will be allocated and the initial values will be set. Initialization process can be compared to constructors in other object oriented languages. It is nothing but a function which enables setting values as shown above.

Complete Example

Let’s look at a complete example using object orientation in Lua.

When you run the above program, below output will be displayed.

Inheritance in Lua

Inheritance is the process of extending simple base objects like shape to rectangles, squares and so on. It is often used in the real world for sharing and extending the basic properties and functions.

Let’ see a simple class extension. We have a class as shown below.

You can extend the shape to a square class as shown below.

Over-riding Base Functions

Base class functions can be overridden, that is instead of using the function in the base class, and derived class can have its own implementation as shown below

Inheritance Complete Example

You can extend the simple class implementation in Lua as shown above using another new method with the help of metatables. All the member variables and functions of base class will be retained in the derived class.

When you run the above program, below output will be displayed

In the above example, we have created two derived classes − Rectangle and Square from the base class Square. It is possible to override the functions of the base class in derived class. In this example, derived class overrides the function printArea.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Lua (programming language) Topics