Single Inheritance

Bhavik Patel
Jul 21, 2017 · 3 min read

(Disclaimer: the example used to describe this topic uses some political material)

I’ve been learning Ruby for sometime and I recently started to learn more about Object Oriented Programming. OOP with Ruby only has a single inheritance model, which means a subclass can only inherit behaviors from one superclass. I originally thought this was limiting behavior, but it’s not because Ruby uses modules to mix-in additional behaviors in order to fill in the gaps.

The benefits of having a inheritance is that we don’t need to recreate a lot of additional code. If we need our objects to have more specific features, we can create a subclass with those behaviors, but we won’t need to rewrite all the previous code again. We can just inherit those attributes and behaviors from the superclass.

Let’s use Donald Trumps family as an example of how we can learn about single inheritance. The class you inherit from tends to have more general and common behaviors than the subclass. Donald trump doesn’t lack personality nor does he lack particular behaviors, but let’s assume in this example he’s a simple minded individual with couple basic behaviors that all his children can inherit.

class Donald
attr_accessor :age, :height, net_worth
def initialize(age, height, net_worth)
@age = age
@height = height
@net_worth = net_worth
end
def make_political_statement
puts "We're going to make America great again
end
def lie!(statement)
puts "I never said, #{statement}"
end
def russia_responce
puts "We've never worked with anyone from Russia
end
endendclass Ivana
def initialize(age, height, years_modeling)
@age = age
@height = height
@years_modeling = years_modeling
end
end
class DonaldJr < Donald
end
class Ivanka < Donald
end
class Eric < Donald
end

Let’s first take a look at Donald’s children from his first wife Ivana and the code above. I’ve set it up so that each of the children inherits all the behaviors and attributes that their father has. This is super helpful because now we don’t have to re-write all that code for each of his children. But as well all know, we don’t just inherit things from only one of our parents. So what about their mother Ivana?

Ruby doesn’t allow us to inherit from multiple classes, and we want each of the children to have all the behaviors from Donald & some from Ivana. Ruby’s solution to this is to create modules and mix them in when appropriate. Let’s add to the code and do a little refactoring to demonstrate this concept.

module Modeling
def years_modeled
@years_modeling
end
end
class Donald
attr_accessor :age, :height, :net_worth
def initialize(age, height, net_worth)
@age = age
@height = height
@net_worth = net_worth
end
def make_political_statement
puts "We're going to make America great again"
end
def lie!(statement)
puts "I never said, #{statement}"
end
def russia_responce
puts "We've never worked with anyone from Russia"
end
end
class Ivana
include Modeling
def initialize(age, height, net_worth, years_modeling)
@age = age
@height = height
@years_modeling = years_modeling
end
end
class DonaldJr < Donald
end
class Ivanka < Donald
include Modeling
attr_accessor :years_modeling
def initialize(age, height, net_worth, years_modeling)
super(age, height, net_worth)
@years_modeling = years_modeling
end
endclass Eric < Donald
end
evlperson1 = Ivanka.new(35, 71, 1_000_000_000, 2)
p evlperson1.years_modeling # => 2

Ivana was a model back in the day and so was Ivanka. Instead of changing the inheritance structure, we can have them both inherit the behavior of returning the number of years they modeled by mixing in a Module to each of their classes. This is the flexibility that Modules provides us. Since Melania was a model as well, we could include this module as well within in own class.

At this point you might be thinking about what all the differences between classes and modules are, and when to use them. Here are some quick guidelines.

  1. When using a class, a subclass can only inherit from one superclass. Ivanka can only inherit from one of her parents (Donald or Ivana). This isn’t a problem though, because we can use modules to fill in any gaps.
  2. There aren’t limitations to how many modules we can mix into a class.
  3. If the relationship seems more like a behavior for the object, choose a module. If the relationship seems like a relative, choose class inheritance.
  4. You cannot create objects out of modules. Use them for grouping common methods together as well as namespacing.
)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade