
- Lua Tutorial
- Lua - Home
- Lua Basics
- Lua - Overview
- Lua - Environment
- Lua - Basic Syntax
- Lua - Comments
- Lua - Print Hello World
- Lua - Variables
- Lua - Data Types
- Lua - Operators
- Lua - Loops
- Lua - Generic For
- Lua - Decision Making
- Lua - Date and Time
- Lua Functions
- Lua - Functions
- Lua - Multiple Results
- Lua - Named Arguments
- Lua - Default/Optional Arguments
- Lua - Closures
- Lua - Uses of Closures
- Lua - Local Functions
- Lua - Anonymous Functions
- Lua - Functions in Table
- Lua - Proper Tail Calls
- Lua Strings
- Lua - Strings
- Lua - String Concatenation
- Lua - Loop Through String
- Lua - String to Int
- Lua - Split String
- Lua - Check String is NULL
- Lua Arrays
- Lua - Arrays
- Lua - Multi-dimensional Arrays
- Lua - Array Length
- Lua - Iterating Over Arrays
- Lua - Slicing Arrays
- Lua - Sorting Arrays
- Lua - Merging Arrays
- Lua - Sparse Arrays
- Lua - Searching Arrays
- Lua - Resizing Arrays
- Lua - Array to String Conversion
- Lua - Array as Stack
- Lua - Array as Queue
- Lua - Array with Metatables
- Lua - Immutable Arrays
- Lua - Shuffling Arrays
- Lua Iterators
- Lua - Iterators
- Lua - Stateless Iterators
- Lua - Stateful Iterators
- Lua - Built-in Iterators
- Lua - Custom Iterators
- Lua - Iterator Closures
- Lua - Infinite Iterators
- Lua - File Iterators
- Lua - Table Iterators
- Lua - Numeric Iterators
- Lua - Reverse Iterators
- Lua - Filter Iterators
- Lua - Range Iterators
- Lua - Chaining Iterators
- Lua Tables
- Lua - Tables
- Lua - Tables as Arrays
- Lua - Tables as Dictionaries
- Lua - Tables as Sets
- Lua - Table Length
- Lua - Table Iteration
- Lua - Table Constructors
- Lua - Loop through Table
- Lua - Merge Tables
- Lua - Nested Tables
- Lua - Accessing Table Fields
- Lua - Copy Table by Value
- Lua - Get Entries from Table
- Lua - Table Metatables
- Lua - Tables as Objects
- Lua - Table Inheritance
- Lua - Table Cloning
- Lua - Table Sorting
- Lua - Table Searching
- Lua - Table Serialization
- Lua - Weak Tables
- Lua - Table Memory Management
- Lua - Tables as Stacks
- Lua - Tables as Queues
- Lua - Sparse Tables
- Lua Lists
- Lua - Lists
- Lua - Inserting Elements into Lists
- Lua - Removing Elements from Lists
- Lua - Iterating Over Lists
- Lua - Reverse Iterating Over Lists
- Lua - Accessing List Elements
- Lua - Modifying List Elements
- Lua - List Length
- Lua - Concatenate Lists
- Lua - Slicing Lists
- Lua - Sorting Lists
- Lua - Reversing Lists
- Lua - Searching in Lists
- Lua - Shuffling List
- Lua - Multi-dimensional Lists
- Lua - Sparse Lists
- Lua - Lists as Stacks
- Lua - Lists as Queues
- Lua - Functional Operations on Lists
- Lua - Immutable Lists
- Lua - List Serialization
- Lua - Metatables with Lists
- Lua Modules
- Lua - Modules
- Lua - Returning Functions from Modules
- Lua - Returning Functions Table from Modules
- Lua - Module Scope
- Lua - SubModule
- Lua - Module Caching
- Lua - Custom Module Loaders
- Lua - Namespaces
- Lua - Singleton Modules
- Lua - Sharing State Between Modules
- Lua - Module Versioning
- Lua Metatables
- Lua - Metatables
- Lua - Chaining Metatables
- Lua Coroutines
- Lua - Coroutines
- Lua File Handling
- Lua - File I/O
- Lua - Opening Files
- Lua - Modes for File Access
- Lua - Reading Files
- Lua - Writing Files
- Lua - Closing Files
- Lua - Renaming Files
- Lua - Deleting Files
- Lua - File Buffers and Flushing
- Lua - Reading Files Line by Line
- Lua - Binary File Handling
- Lua - File Positioning
- Lua - Appending to Files
- Lua - Error Handling in File Operations
- Lua - Checking if File exists
- Lua - Checking if File is Readable
- Lua - Checking if File is Writable
- Lua - Checking if File is ReadOnly
- Lua - File Descriptors
- Lua - Creating Temporary Files
- Lua - Working with Large Files
- Lua Advanced
- Lua - Error Handling
- Lua - Debugging
- Lua - Garbage Collection
- Lua - Object Oriented
- Lua - Web Programming
- Lua - Database Access
- Lua - Game Programing
- Lua Useful Resources
- Lua - Quick Guide
- Lua - Useful Resources
- Lua - Discussion
Lua - Tables Inheritance
In Lua, we can define a class using Table as we've seen in Table as Objects. In this chapter we'll look at ways to inherit a Lua Table as an Object.
Object creation via new function
Lua provides metamethods to extend properties of a table. __index is one of the special metamethod which we can use to inherit properties of a table. For example, whenever we create a new object, we can set the metatable as self and update the __index method to self as well as shown below−
function Shape:new (o) -- create object if not provided o = o or {} setmetatable(o, self) self.__index = self return o end
Let's consider a scenario of shapes like circle, rectangle and square. The shapes can have a common property Area. So, we can extend other shapes from the base object shape with the common property area. Each of the shapes can 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.
Create Shape Class as Base Class
-- Base Class Shape = { -- common area property area = 0, -- printArea function printArea = function(self) print("Area: ",self.area) end } -- new method to create a new object function Shape:new (o) -- create object if not provided o = o or {} setmetatable(o, self) self.__index = self return o end
Create Rectangle Class as Sub Class
-- create Rectangle object by extending Shape class 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 self.area = length * breadth return o end
Create Circle Class as Sub Class
-- create Circle object by extending Shape class Circle = Shape:new() -- Derived class method new function Circle:new (o,radius) o = o or Shape:new(o) setmetatable(o, self) self.__index = self self.area = 3.14 * radius * radius return o end
Create Square Class as Sub Class
-- create Square object by extending Shape class Square = Shape:new() -- Derived class method new function Square:new (o,side) o = o or Shape:new(o) setmetatable(o, self) self.__index = self self.area = side * side return o end
Example - Using Rectangle as Subclass
Following is the complete example of using Rectangle inheriting Shape class. We're creating a instance of Rectangle and printing its area.
main.lua
-- Base Class Shape = { -- common area property area = 0, -- computeArea function getArea = function(self) return self.area end } -- new method to create a new object function Shape:new (o) -- create object if not provided o = o or {} setmetatable(o, self) self.__index = self return o end -- create Rectangle object by extending Shape class 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 self.area = length * breadth return o end -- create object of Rectangle rectangle = Rectangle:new(nil, 10, 5) -- print area of rectangle print("Area of rectangle: ", rectangle:getArea())
Output
When the above code is built and executed, it produces the following result −
Area of rectangle: 50
Example - Using Square as Subclass
Following is the complete example of using Square inheriting Shape class. We're creating a instance of Square and printing its area.
main.lua
-- Base Class Shape = { -- common area property area = 0, -- computeArea function getArea = function(self) return self.area end } -- new method to create a new object function Shape:new (o) -- create object if not provided o = o or {} setmetatable(o, self) self.__index = self return o end -- create Square object by extending Shape class Square = Shape:new() -- Derived class method new function Square:new (o,side) o = o or Shape:new(o) setmetatable(o, self) self.__index = self self.area = side * side return o end -- create object of Square square = Square:new(nil, 10) -- print area of the square print("Area of square: ", square:getArea())
Output
When the above code is built and executed, it produces the following result −
Area of square: 100
Example - Using Circle as Subclass
Following is the complete example of using Circle inheriting Shape class. We're creating a instance of Circle and printing its area.
main.lua
-- Base Class Shape = { -- common area property area = 0, -- computeArea function getArea = function(self) return self.area end } -- new method to create a new object function Shape:new (o) -- create object if not provided o = o or {} setmetatable(o, self) self.__index = self return o end -- create Circle object by extending Shape class Circle = Shape:new() -- Derived class method new function Circle:new (o,radius) o = o or Shape:new(o) setmetatable(o, self) self.__index = self self.area = 3.14 * radius * radius return o end -- create object of Circle circle = Circle:new(nil, 10) -- print area of the circle print("Area of Circle: ", circle:getArea())
Output
When the above code is built and executed, it produces the following result −
Area of Circle: 314.0