Inheritance and Modules
Super Calls
super lets a subclass call the parent version of a method.
Super Calls
super_calls.rb
class Account
def initialize(owner)
@owner = owner
end
def label
"account: #{@owner}"
end
end
class PremiumAccount < Account
def initialize(owner, tier)
super(owner)
@tier = tier
end
def label
"#{super} #{@tier}"
end
end
tier =
account = PremiumAccount.new("Ada", tier)
puts account.label
class Account
def initialize(owner)
@owner = owner
end
def label
"account: #{@owner}"
end
end
class PremiumAccount < Account
def initialize(owner, tier)
super(owner)
@tier = tier
end
def label
"#{super} #{@tier}"
end
end
tier =
account = PremiumAccount.new("Ada", tier)
puts account.label
class Account
def initialize(owner)
@owner = owner
end
def label
"account: #{@owner}"
end
end
class PremiumAccount < Account
def initialize(owner, tier)
super(owner)
@tier = tier
end
def label
"#{super} #{@tier}"
end
end
tier =
account = PremiumAccount.new("Ada", tier)
puts account.label
super
Calling `super` reuses parent-class setup or behavior before adding subclass details.