Tl;dr: Ruby is an object-oriented language and allows for classes to take in the object characteristics of another class using the “<” symbol in the inheriting class’ Class definition file
In this blogpost, I write about object inheritance in Ruby. Ruby is an object-oriented language, and one of the key features of an object-oriented language is its ability to take in the object characteristics of another class. We accomplish this by having the “subclass” inherit the characteristics of a “superclass”. Once this model is defined, then your program is able to use the class characteristics (definitions, functions, etc.) of both the sub AND super classes.
What are Classes in Ruby?
Before we jump into object inheritance, let’s quickly review the concept of a “Class”. A Class is effectively a “blueprint” for the objects you will be creating in that class. Put another way, this is where you define what type of object you are creating - so, if, as an example, you were creating individual object instances of wines (i.e. Chardonnay, Syrah), you might create a Class of “Wine” since both Chardonnay and Syrah are both a type of wine.
class Wine
end
However, you might also have different types of alcohol! Not only do you have wines, but you also have tequilas and whiskeys. Those are not types of wines, so you might also have another class of “Liquor” that has definitions that broadly apply to tequilas, whiskeys AND wines.
class Liquor
end
*Why Object Inheritance? *
Reusability is a key part of object inheritance. It allows us as programmers to not have to duplicate code. Coming back to our example of liquor and the types of liquor, there are characteristics that wine, tequila and whiskey will all have or share: price, where it’s from, alcohol content, etc.. We wouldn’t want to copy that code for EVERY SINGLE type of alcohol!
So for those reusable characteristics, we would define those in the superclass, like so:
class Liquor
attr_accessor :price, :type_liquor, :region, :alcohol_content
def initialize(price, type_liquor, region, alcohol_content)
@price = price
@type_liquor = type_liquor
@region = region
@alcohol_content =alcohol_content
end
def alcohol
"Careful, this will get you buzzed."
end
end
Superclass and Subclass
In the hierarchy of object inheritance, we have what are called “Superclass” and “Subclass”. The Superclass is the class from which the Subclass “inherits” the object characteristics. In our example, “Liquor” would be a Superclass, and the type of liquor would be a “Subclass”.
Syntax for Object Inheritance
We use the inequality symbol <
(or shovel) for the subclass to inherit the superclass. We can also define characteristics, methods, specific to the Wine class in the Wine file as well.
Here is an example:
class Wine < Liquor
def how_to_drink
"To drink the wine, take a small sip and swirl the wine in your mouth"
end
end
And it’s as simple as that! Now, all the characteristics we defined in our Liquor class are available to our Wine class.
Now let’s try it out in IRB!
First, let’s make sure that Wine has inherited the superclass of Liquor. You can use a method called #superclass. When appended to a class, it will tell you that class’ superclass:
2.7.4 :001 > require_relative 'liquor'
=> true
2.7.4 :002 > require_relative 'wine'
=> true
2.7.4 :003 > Wine.superclass
=> Liquor
Next, let’s initialize an instance of Wine, called “chardonnay”.
2.7.4 :004 > chardonnay = Wine.new("$10", "wine", "California", "14%")
=> #<Wine:0x00007fc25d95a5c0 @price="$10", @type_liquor="wine", @regio...
*Note how all the parameters we are giving to chardonnay through the Wine class were actually defined up in the Liquor class. Recall also that the Wine class has no object parameters defined. Wine has inherited all the characteristics of Liquor, and we’re able to create the object with parameters defined in the superclass.
Coming back to our method #superclass - if we were encountering the object “chardonnay” and needed to learn more about its class hierarchy, we could string #class and #superclass to traverse the class hierarchy.
2.7.4 :009 > chardonnay.class
=> Wine
2.7.4 :010 > chardonnay.class.superclass
=> Liquor
Moving along, recall we defined a method, #how_to_drink, in our Wine class. Let’s see what that method says about how to drink our chardonnay:
2.7.4 :006 > chardonnay.how_to_drink
=> "To drink the wine, take a small sip and swirl the wine in your mouth"
Solid advice, but more importantly, our class method works!
Finally, let’s call the method #alcohol that we defined in our superclass, Liquor, on our object chardonnay:
2.7.4 :005 > chardonnay.alcohol
=> "Careful, this will get you buzzed."
Success!
[Aside: you might be wondering what happens when both the superclass and subclass have a method with the same name. In this situation, the subclass’ method will overwrite the inherited method. So, if Liquor.rb also had #how_to_drink method
, the method #how_to_drink
in Wine.rb would overwrite Liquor’s. ]
Top comments (0)