0Pricing
Lua Academy · Lesson

Calling Parent Methods with super

Explicitly call base class methods for cooperative initialization.

Why Super Calls?

When a child overrides a parent method but still needs the parent's logic, it calls the parent method explicitly. Lua has no super keyword — you reference the parent table directly: Parent.method(self, args).

local Base={}
Base.__index=Base
function Base:describe()
  return "Base[name="..self.name.."]"
end

local Child={}
Child.__index=Child
setmetatable(Child,{__index=Base})

function Child:describe()
  local parentDesc = Base.describe(self)  -- super call
  return parentDesc .. " + Child[extra=true]"
end

local c=setmetatable({name="X"},Child)
print(c:describe())

Super in Constructor

Child constructors call parent constructors via Parent.new() or Parent._init(self,...). This ensures the parent's initialization runs for every child instance.

local Animal={}
Animal.__index=Animal
function Animal._init(self,name,sound)
  self.name=name; self.sound=sound; self.alive=true
end

local Dog={}
Dog.__index=Dog; setmetatable(Dog,{__index=Animal})
function Dog.new(name,breed)
  local self=setmetatable({},Dog)
  Animal._init(self,name,"Woof")  -- super init
  self.breed=breed
  return self
end

local d=Dog.new("Rex","Lab")
print(d.name,d.sound,d.breed)

All lessons in this course

  1. Single Inheritance with __index Chaining
  2. Calling Parent Methods with super
  3. Mixin Patterns
  4. Overriding and Polymorphism
← Back to Lua Academy