Inherited Classes vs Modules

What are they and what’s the difference?

Seriously though, it seems like everyone I asked about tells me that the two things are more or less the same. So why make a distinction in the first place? The answer always seems to come back to a pretty simple concept:

It depends on what you are modeling

Yep, it really is that easy. Let’s look at a couple of examples to get a better idea.

Humans

Pretend we are creating a program to build humans. Dusting off the cover of our high school biology books, we remember that humans are part of the Animalia Kingdom. So we create an animal class.

Class Animal
    def cellular_structure
puts "Hey there, I'm a cell!"
puts "Woah, there are lots of us here!"
end
    def i_am_hungry
puts "Go eat something because you can't produce your own"
end
end

Now, whether we are building a Bird class or a Human class, they will both inherit methods from the animal class. So they will both be multicellular and they both have to find their own food.

If we continue down the genus branches, we hit a hominid class as well.

Class Hominid < Animal
   def omnivores
puts "I will eat almost anything"
end
   def gestation
puts "See you in 9 months!"
end
   def thumbs
puts "Woah, these are super useful!"
end
end

Since Hominids inherit from the Animal class, in addition to being able to eat both plants and meats and use their thumbs, they will still be able to call the multicellular methods as well. While a Bird class will be multicellular as well, it can not use a method for thumbs because it does not inherit the Hominid class.

Super Humans

While we like to pretend we are all special snowflakes, most people are going to be about the same. Let’s say we have a normal instance of a human class.

peter_parker = Human.new

Peter is multicellular and has thumbs because he inherits from the Hominid class which inherits from the Animal class. He can have access to all of these methods because of this inheritance.

But OSCORP is up to no good and has been cooking up some weird experiments…

Module RadioactiveSpider
   def super_strength
puts "I'm 100x stronger!"
end
   def spidey_sense
puts "It tingles!"
end
   def web_shooter
puts "Weeeeeeeeeeeee!!"
end
end

Oh no! The RadioactiveSpider module broke loose and has attached itself to our human class!

class Human
include RadioactiveSpider
  ...
end
peter_parker = Human.new

Peter Parker is still human, but now he has can use all of the RadioactiveSpider’s amazing super power methods! POW!

But wait! We’re not done yet!

class Pig
include RadioactiveSpider

...
end
spider_pig = Pig.new

Even though Spider-Pig doesn’t have thumbs because he inherits from a different class from Peter, he too can now shoot webs. We could extend the methods of the RadioactiveSpider module to any class and they will instantly have super power as well.

The Takeaway

Inherited classes and modules really are very similar. Generally it is best to think about the domain you are trying to model. It would be weird to think that all humans should inherit from the RadioactiveSpider. Instead it is used as an insertion to allow for additional methods.