A Brief Overview Of Class Inheritance In Ruby

declan mulligan
3 min readJan 22, 2018

In Ruby, inheritance is a relation between two classes that allows one to utilize the code of the other. Typically, this relation takes the form of a hierarchy, in which classes that are lower down are able to take advantage of the code of those classes that are higher up. In other words, they are able to inherit and modify features of other classes that are higher up the hierarchical chain. It is worth noting that in Ruby, a class can only inherit from a single other class.

Example of a hierarchy of classes

Using the above hierarchy as an example, let us define two class:

class Animal
def alive?
puts 'This animal is alive.'
end
end
class Mammal
end

In order for the Mammal class to inherit from the Animal class, we must add:

class Animal
def alive?
puts 'This animal is alive.'
end
end
class Mammal<Animal
end

Now, we call the .alive? method on an instance of the Mammal class as though it was one of its instance methods, even though it is not. In this example, the Mammal class is a sub-class of the animal class, which is in turn called the super class. The sub-classes inherit from the super class. Typically, the super-class is the largest class, with many sub-classes that don’t so much present their own code as modify code inherited from the super class.

On the other hand, there are times when we do not want a sub-class to inherit the exact behaviors of its super-class. In these situations, we can still use the majority of the code from the super-class to build our sub-class while altering or modifying the parts that we wish to change. For example if these are our classes:

class Mammal
def make_noise
puts'Bark'
end
def alive?
puts'This animal is alive.'
end
endclass Cat
end

We can write the Cat class so that it meow rather than barks, like so:

class Cat<Mammal
def make_noise
puts'Meow'
end
end

Now, if we call .make_noise on an instance of our Cat class, we will get back ‘Meow’ rather than ‘Bark, while if we called the .make_noise method on an instance of our Mammal class, we would get ‘Bark.

dog=Mammal.new
dog.make_noise
=>'Bark'
cat=Cat.new
cat.make_noise
=>'Meow'

This ability to overwrite information from the class that is being inherited makes the inheritance mechanic very usful, as you can import the majority of the code for a class and then append the data as you see fit. This is significantly easier than writing out multiple classes that are composed of mostly the same code. Essentially, inheritance allows us to create a refinement of another, different class.

It is important to note that while inheritance works on instance methods, it cannot transfer instance variables. This is because instance variables are not defined by an objects class. They are simply pieces of information that are injected into the object during its instantiation. They only exist once a value is assigned to them.

Sources:

http://rubylearning.com/satishtalim/ruby_inheritance.html

https://launchschool.com/books/oo_ruby/read/inheritance

--

--