Inheritance and Modules
Super Calls
super lets a subclass call the parent version of a method.
super
Calling `super` reuses parent-class setup or behavior before adding subclass details.
Super Calls
super_calls.rb
Replay: real traced execution (multi-file project)
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 = "gold"
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 = "silver"
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 = "platinum"
account = PremiumAccount.new("Ada", tier)
puts account.label
tier ← gold
22tier→ gold = "gold" #@tier="silver", "platinum"23account = PremiumAccount.new("Ada", tiergold)def initialize(owner, tier)
11class PremiumAccount < Account12 def initialize(ownerAda, tiergold)13 super(owner)14 @tier = tier@owner ← Ada
1class Account2 def initialize(ownerAda)3 @owner→ Ada = ownerAda4 end@tier ← gold
13 super(owner)14 @tier→ gold = tiergold15endaccount ← ⟨PremiumAccount A @owner="Ada", @tier="gold"⟩
22tier = "gold" #@tier="silver", "platinum"23account→ ⟨PremiumAccount A @owner="Ada", @tier="gold"⟩ = PremiumAccount.new("Ada", tiergold)2425puts account.labelaccount: Ada golddef label
6def label7 "account: #{@ownerAda}"8endputs account.label
25puts account.labelaccount: Ada goldoutputaccount: Ada gold
tier ← silver
22tier→ silver = "silver"23account = PremiumAccount.new("Ada", tiersilver)def initialize(owner, tier)
11class PremiumAccount < Account12 def initialize(ownerAda, tiersilver)13 super(owner)14 @tier = tier@owner ← Ada
1class Account2 def initialize(ownerAda)3 @owner→ Ada = ownerAda4 end@tier ← silver
13 super(owner)14 @tier→ silver = tiersilver15endaccount ← ⟨PremiumAccount A @owner="Ada", @tier="silver"⟩
22tier = "silver"23account→ ⟨PremiumAccount A @owner="Ada", @tier="silver"⟩ = PremiumAccount.new("Ada", tiersilver)2425puts account.labelaccount: Ada silverdef label
6def label7 "account: #{@ownerAda}"8endputs account.label
25puts account.labelaccount: Ada silveroutputaccount: Ada silver
tier ← platinum
22tier→ platinum = "platinum"23account = PremiumAccount.new("Ada", tierplatinum)def initialize(owner, tier)
11class PremiumAccount < Account12 def initialize(ownerAda, tierplatinum)13 super(owner)14 @tier = tier@owner ← Ada
1class Account2 def initialize(ownerAda)3 @owner→ Ada = ownerAda4 end@tier ← platinum
13 super(owner)14 @tier→ platinum = tierplatinum15endaccount ← ⟨PremiumAccount A @owner="Ada", @tier="platinum"⟩
22tier = "platinum"23account→ ⟨PremiumAccount A @owner="Ada", @tier="platinum"⟩ = PremiumAccount.new("Ada", tierplatinum)2425puts account.labelaccount: Ada platinumdef label
6def label7 "account: #{@ownerAda}"8endputs account.label
25puts account.labelaccount: Ada platinumoutputaccount: Ada platinum