class A
def hoge
puts "Aクラスのhoge"
end
end
class B < A
def hoge
puts "Bクラスのhoge"
end
end
cls = B.new
cls.hoge
# 結果
# Bクラスのhoge
ここまでは通常の継承とオーバーライドである。
しかし、Rubyではオーバーライドするメソッドに super を入れると、オーバーライド元のメソッドを実行することができる。これは面白い挙動だ。
class A
def hoge
puts "Aクラスのhoge"
end
end
class B < A
def hoge
super
puts "Bクラスのhoge"
end
end
cls = B.new
cls.hoge
# 結果
# Aクラスのhoge
# Bクラスのhoge
super は、いつでも呼び出すことができるうえ、何度も呼び出すことができる。
ちなみに、オーバーロードはないらしい。