Inheritance – classes build on each other
Dogs, cows and owls have a lot in common: a name, a sound, they introduce themselves. Instead of writing everything three times, you build a general class Animal and let more specific classes inherit from it. The little < means “inherits from”:
class Animal
attr_reader :name
def initialize(name)
@name = name
end
def sound
"..."
end
def introduce
"I am #{name} and I say #{sound}"
end
end
class Cow < Animal
def sound
"Moo!"
end
end
class Owl < Animal
end
puts Cow.new("Bertha").introduce
puts Owl.new("Hoot").introduceCow and Owl get everything from Animal: initialize, name and introduce. The cow only overrides sound – it defines the method anew, and its version wins. The owl overrides nothing and therefore says .... The beauty of it: introduce is written only once, in Animal, and still calls the right sound.
If a subclass wants to extend the parent's method rather than replace it, it calls the parent's version with super:
class Dog < Animal
def initialize(name, breed)
super(name)
@breed = breed
end
def sound
"Woof!"
end
def introduce
super + " – I'm a #{@breed}"
end
end
rex = Dog.new("Rex", "dachshund")
rex.introduceIn initialize the dog passes the name on to Animal with super(name) and also remembers its breed. In introduce it takes the sentence Animal builds and appends something.
Ruby tells you at any time how classes are related. is_a? asks “is this (also) a …?”, superclass names the parent class, and ancestors shows the whole family line:
p rex.is_a?(Dog)
p rex.is_a?(Animal)
p rex.is_a?(Cow)
p Dog.superclass
p Animal.superclass
Dog.ancestors.first(3)Every class has a parent class – all the way up to Object (and behind it BasicObject). That's why every object can do things like to_s, inspect or class: it inherits them from Object.
You'll meet inheritance often in this course: class MySite < Sinatra::Base (lesson 54), class TestEntry < Minitest::Test (lesson 111) or class TimelogError < StandardError (lesson 115) – each time you inherit a pile of abilities from a finished class and only add your own.
One last piece of advice: inheritance fits when you can say “is a” – a dog is an animal. If quite different classes should merely share one ability (being comparable, being countable), modules are the better choice. That's what the next lesson is about.
ForestAnimal. Write the class RedFox, which inherits from ForestAnimal and whose sound is "Chunky Bacon!". It should not write introduce itself but inherit it. Test it with RedFox.new("Chunky").introduce. (If Ruby reports superclass mismatch, you created RedFox before without < ForestAnimal: a class cannot change its parent. Reload the page then.)class ForestAnimal
attr_reader :name
def initialize(name)
@name = name
end
def sound
"..."
end
def introduce
"I am #{name} and I say #{sound}"
end
end
# class RedFox < ForestAnimal
# ...
# end
# RedFox.new("Chunky").introduce
super, like the dog above: write Hen, which inherits from FarmAnimal. Its sound is "Cluck!", and its introduce takes the parent's sentence and appends - and I lay eggs. Hen.new("Berta").introduce then gives I am Berta and I say Cluck! - and I lay eggs.class FarmAnimal
attr_reader :name
def initialize(name)
@name = name
end
def sound
"..."
end
def introduce
"I am #{name} and I say #{sound}"
end
end
# class Hen < FarmAnimal
# ... # sound is "Cluck!", introduce adds " - and I lay eggs" to the parent's sentence
# end
# Hen.new("Berta").introduce