In Ruby, a class can inherit from one other class. This is called single inheritance. Some languages have support for multiple inheritance, which means a class can inherit from multiple classes. Ruby has ways of providing functionality that’s similar to multiple inheritance, which we'll discuss in the next lesson. For now, we'll focus on the basics of single inheritance in Ruby.
Note that using class inheritance is not required for this section's independent project. Nor are you required to use multiple classes and class inheritance during your in-class projects, either. However, inheritance is an essential part of Ruby and many other languages, so it's very important that you learn this concept.
We’ve already experimented a bit with the
class() method. For instance:
> "hello".class() => String
Ruby also has a
superclass() method. In Ruby, a “parent" class is a superclass while a “child class" is a subclass. Let’s take a look at the
String and work our way up the chain of inheritance.
> String.superclass() => Object > Object.superclass() => BasicObject > BasicObject.superclass() => nil
String inherits from
Object which inherits from
BasicObject is at the very top of the inheritance chain and doesn’t have its own superclass. In fact,
BasicObject is at the top of the inheritance chain for every class in Ruby.
A class inherits all the methods of its superclass and that class's superclass and so on. Remember when we called
Integers to see what methods they could use? Many of those methods don’t belong to
Integer. Instead, they are either inherited from other classes or mixed in from modules. (We’ll go over modules and mixins in the next lesson.)
In fact, we can use simple arithmetic to see
String methods without the methods that are inherited from its superclasses.
> "hello".methods() - Object.methods()
methods() returns an array of methods and we can subtract one array from another to see the difference between the two, so this will return a list of methods unique to instances of the
We can use inheritance in our own classes to keep our code DRY. For instance, let’s say we have an application that has a
Dog class and a
Cat class. Dogs and cats share many common traits so we could create a
Mammal superclass. Here’s how we can use inheritance so both
Dogs inherit from
class Mammal def number_of_legs 4 end end class Cat < Mammal def meow "Meow!" end end class Dog < Mammal def bark "Bark!" end end
We’ve created three classes:
Dog. We use the
< symbol to show that both the
Dog classes inherit from the
Mammal class, like this:
class Cat < Mammal
Now we can call all of
Mammal’s methods on both
> cat = Cat.new() > cat.number_of_legs() => 4 > dog = Dog.new() > dog.number_of_legs() => 4
meow() and only
We can also choose to have a custom class inherit from a built-in Ruby class. For instance, we might want our
Word class with its
scramble() method to inherit from
String. If so, we can do this:
class Word < String
Our applications are sufficiently simple right now, which means we won’t need to use class inheritance much just yet. However, inheritance is an important part of Ruby and it’s regularly utilized in tools we’ll be using such as ActiveRecord, Rails, various gems, and the language itself. Take the time to explore inheritance now if you have the chance to refactor a project. Once again, you won’t be expected to utilize inheritance in this course section's independent project.
Now that you know the basics of inheritance, let's explore modules and mixins.
BasicObject: The class at the very top of the inheritance chain in Ruby.
Inheritance: The process of one class passing down information to a child class.
Multiple inheritance: A class can inherit from multiple classes.
Single inheritance: A class can only inherit from one other class.
Subclass: A child class.
Superclass: A parent class.
class Mammal def number_of_legs 4 end end class Cat < Mammal def meow "Meow!" end end
Cat inherits from
Mammal. We use the
< symbol to denote when one class inherits from another class.
Lesson 12 of 22
Last updated August 7, 2022