🎓 A1Learner
🔍
0 of 52 lessons completed

Lua Inheritance

Inheritance is a way for one class to reuse the methods of another class.

A subclass object can call a parent method

Dog is a subclass of Animal. Dog defines no speak method, so the call falls back to Animal.

local Animal = {}
Animal.__index = Animal
function Animal.new(name)
  local self = setmetatable({}, Animal)
  self.name = name
  return self
end
function Animal:speak()
  return self.name .. " makes a sound"
end
local Dog = {}
Dog.__index = Dog
setmetatable(Dog, {__index = Animal})
function Dog.new(name)
  local self = setmetatable({}, Dog)
  self.name = name
  return self
end
local d = Dog.new("Rex")
print(d:speak())

A subclass method replaces the parent method

Dog now defines its own speak method. The dog object uses the Dog version, while an animal object keeps the Animal version.

local Animal = {}
Animal.__index = Animal
function Animal.new(name)
  local self = setmetatable({}, Animal)
  self.name = name
  return self
end
function Animal:speak()
  return self.name .. " makes a sound"
end
local Dog = {}
Dog.__index = Dog
setmetatable(Dog, {__index = Animal})
function Dog.new(name)
  local self = setmetatable({}, Dog)
  self.name = name
  return self
end
function Dog:speak()
  return self.name .. " says Woof"
end
local a = Animal.new("Generic")
local d = Dog.new("Rex")
print(a:speak())
print(d:speak())

A subclass method can call the parent version

Dog speak calls Animal speak by hand and adds more words. The result keeps the parent sentence and adds the bark.

local Animal = {}
Animal.__index = Animal
function Animal.new(name)
  local self = setmetatable({}, Animal)
  self.name = name
  return self
end
function Animal:speak()
  return self.name .. " makes a sound"
end
local Dog = {}
Dog.__index = Dog
setmetatable(Dog, {__index = Animal})
function Dog.new(name)
  local self = setmetatable({}, Dog)
  self.name = name
  return self
end
function Dog:speak()
  return Animal.speak(self) .. " and barks"
end
local d = Dog.new("Rex")
print(d:speak())

Example

local Animal = {}
Animal.__index = Animal
function Animal.new(name)
  local self = setmetatable({}, Animal)
  self.name = name
  return self
end
function Animal:describe()
  return self.name .. " is an animal"
end
function Animal:speak()
  return self.name .. " makes a sound"
end
local Dog = {}
Dog.__index = Dog
setmetatable(Dog, {__index = Animal})
function Dog.new(name)
  local self = setmetatable({}, Dog)
  self.name = name
  return self
end
function Dog:speak()
  return self.name .. " says Woof"
end
function Dog:fetch()
  return self.name .. " fetches the ball"
end
local d = Dog.new("Rex")
print(d:describe())
print(d:speak())
print(d:fetch())

Key points

  • Parent class: A parent class is the class whose methods are reused.
  • Subclass: A subclass is a new class that builds on a parent class and can add its own methods.
  • Index chain: A subclass sets its own __index to itself and its metatable __index to the parent, so a missing method falls back through both tables.
  • Inherited method: A method defined only on the parent can be called on a subclass object.
  • Override: A subclass can define a method with the same name as a parent method, and the subclass version runs for subclass objects.
  • Parent call: A subclass method can call the parent version by naming the parent table and passing self by hand.

In this example, Animal defines describe and speak. Dog is linked to Animal through its metatable, and Dog defines its own speak and a fetch method. A dog object is made. Its describe call falls back to Animal, its speak call uses the Dog version, and its fetch call uses the new Dog method.

Note

Lua has no built-in inheritance keyword, so the chain is built with metatables.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A subclass defines a method with the same name as a parent method. Which version runs for a subclass object?