Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...

Twitter:
@daniel_moralesp

2019-09-20

Inheritance in Ruby Objects

Let's continue our advance in Ruby OOP; this time, we'll be talking about Inheritance. Ruby inheritance is a way to reuse code in Oriented-object programming (OOP). Using Inheritance, a Class can use the methods from other Classes. 

Inheritance


The easy way to understand this is through an example. First, let's create a new file on our text editor, and let's call it inheritance.rb, and we will declare the following classes inside it.

class Circle
 attr_accessor :stroke, :fill, :radious
 
 ## other code here
end
 
class Square
 attr_accessor :stroke, :fill, :side
 
 ## other code here
end
 
class Triangle
 attr_accessor :stroke, :fill, :base, :height
 
 ## other code here
end



In an image, we have something like this:


As you can see, each class repeats the stroke and fill attributes. These attributes correspond to the line and fill with the Figure, so what can we do to not repeat the same attributes in all figures?

Here is where we can use the Inheritance in Ruby. We can create a parent class called "Figure" that will wrap all common methods in all figures. The syntax to say that we're going to inherit from another Class is: in the child class definition, you have to add the operator "<" followed by the name of the parent Class. Example:

class Figure
 attr_accessor :stroke, :fill
end
 
## Inherits from Figure
class Circle < Figure
 attr_accessor :radius
 
 ## other code here
end
 
## Inherits from Figure
class Square < Figure
 attr_accessor :side
 
 ## other code here
end
 
## Inherits from Figure
class Triangle < Figure
 attr_accessor :base, :height
 
 ## other code here
end
 


Let's see it in an image.


In this case, Circle, Square, and Triangle inherit from Figure, which means that they all have the methods defined in Figure (reading and writing methods like stroke and fill). So let's now try to use it.

class Figure
 attr_accessor :stroke, :fill
end
 
## Inherits from Figure
class Circle < Figure
 attr_accessor :radius
 
 ## other code here
end
 
## Inherits from Figure
class Square < Figure
 attr_accessor :side
 
 ## other code here
end
 
## Inherits from Figure
class Triangle < Figure
 attr_accessor :base, :height
 
 ## other code here
end
 
c1 = Circle.new
c1.fill = "red"
puts c1.fill

Let's execute the code from the console.

$ ruby inheritance.rb    
red


As you can see, Circle doesn't have the reading and writing method for "fill", but it works because Circle inherits from the Figure class who already has these methods.

The advantage of using Inheritance is that if you add more methods inside the parent class, all the child classes will automatically receive that method. For example, let's suppose that we want to add this method to the Figure class: x, y, and shadow. What we have to do is this.

class Figure
 attr_accessor :stroke, :fill, :x, :y, :shadow
end

All the child classes will now have these new methods to read and write the attributes x, y, and shadow. 


Class hierarchy in Ruby

A parent class can have multiple subclasses in Ruby, but a child class can have just one parent class. 




However, a class can be a parent of multiple children classes, and at the same time to be a child of another parent class. This is called "single class inheritance" in programming. 

Key terms in Inheritance: 

  • * Superclass: The class whose inherited characteristics are known as a superclass, base class, or parent class.
  • * Subclass: The class derived from another type is a subclass or derived class or child class. You can also add your own objects methods and base class methods and objects, etc.




As you can see, the last rule still applies to the above image (single class inheritance), so the structure is correct. 

Remember, the whole purpose of the Inheritance is to reuse the code inside the parent class (superclass), so we don't have too much to do with code right now. Later we'll be doing other exercises about it. 

I hope you learned a lot from this post.

Thanks for reading
Daniel Morales