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 Class or Classes.
The easy way to understand this is through an example. Let’s create a new file on out text editor and called inheritance.rb, and we are going to declare next 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 is repeating the stroke and fill attributes. These attributes correspond to the line and fill of 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” who will wrap all common methods in all figures. The syntax to say that we’re going to inherit (or extend) from another Class is: to 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 inherits from Figure, which means that they all have the methods defined in Figure (in this case reading and writing methods like stroke and fill). 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 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 Figure class who already has these methods.
The advantage of using inheritance is that if you need to add more methods inside the parent class, all the child classes will receive that method automatically. Let’s suppose that we want to add this methods 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
Now all the child classes will have these new methods to read and write the attributes x, y and shadow.
Class hierarchy in Ruby
In Ruby a parent class can have multiple children classes, 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:
- - Super class: The class whose characteristics are inherited is known as a superclass or base class or parent class.
- - Sub class: The class which is derived from another class is known as a subclass or derived class or child class. You can also add its own objects, methods in addition to base class methods and objects, etc.
As you can see the last rule still applies on 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 (super class), 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 with this post
Thanks for reading
Daniel Morales