Ruby – Inheritance

Here’s an example of inheritance in action:

# Here we define a parent class
class Animal 
  def quack
    puts "unknown animal that makes a quack noise"
  end

  def legs
    puts "this animal has a pair of legs"
  end
  
end

# Here we define a child class of the animal class. In this case we 
# have over-ridden the "quack" method. But it implicitly inherits the "legs" method as-is. 
class Duck   < Animal
  def quack
    puts "Quaaaaaack!"
  end
 

end
 
 
# Here we define another child class of the animal class. In this case we 
# have over-ridden the "quack" method. But it implicitly inherits the "legs" method as-is.  
class Person   < Animal
  def quack
    puts "The person imitates a duck."
  end
end
 

donald = Duck.new
john = Person.new

# the child class's method should be run in both these instances. 
donald.quack
john.quack
 
# The parent class's method should end up being run in these two instances. 
donald.legs
john.legs


mystery_animal = Animal.new
mystery_animal.quack
mystery_animal.legs

The above outputs:

PS C:\Temp\irb> ruby .\inhertancet.rb
Quaaaaaack!
The person imitates a duck.
this animal has a pair of legs
this animal has a pair of legs
unknown animal that makes a quack noise
this animal has a pair of legs