Introduction

Welcome back! In this part of our Ruby Class Basics Review, we’ll dive into inheritance, a core concept in Ruby’s object-oriented programming (OOP). Inheritance enables code-sharing between classes, making our code more efficient and easier to read.

This lesson covers how inheritance works in Ruby, including attribute and method inheritance, along with the super keyword to access superclass functionality. Ready to dive in? Let’s get started!

Defining Inheritance

Inheritance in Ruby allows us to create a subclass that inherits attributes and methods from a superclass. This approach is especially useful when multiple classes share common features or behaviors.

Let’s explore with a superclass named Vehicle and a subclass named Car:

In this example, Car inherits properties from Vehicle. Ruby supports several types of inheritance, but here we’re focusing on single inheritance, where a subclass has a single superclass.

Attribute Inheritance with attr_reader

With attribute inheritance, a subclass can inherit instance variables from its superclass.

The Musician class inherits the @name attribute from Artist, while also introducing its own attribute, @instrument. Getters can be conveniently defined using attr_reader to access instance variables of each class.

Method Inheritance

Similar to attributes, method inheritance lets a subclass use methods defined in its superclass without redefining them.

In the example below, the Car class inherits the start method from the Vehicle class:

In this case, Car can use the start method inherited from Vehicle.

Understanding the `super` Keyword

The super keyword is essential in Ruby inheritance, allowing a subclass to call methods from its superclass. super is especially useful in method overrides or in initialize methods to ensure that superclass functionality is included in the subclass.

Here’s how super can be used to call an overridden method in the superclass and combine it with new behavior in the subclass:

Similarly, in initialize, super allows us to ensure that both superclass and subclass attributes are set up properly:

In both examples, super allows the subclass to build upon the superclass’s logic, resulting in flexible and reusable code.

Using self.class for Shared Class Methods and Variables

In Ruby, self.class allows instance methods to dynamically call class methods or access shared data. For example, we can track the total number of books checked out in a library system:

In this example:

  • @books_checked_out is a class instance variable, storing data shared among all instances of the Library class.
  • class << self opens the singleton class, allowing us to define class-level methods like books_checked_out and checkout_book in a grouped and organized manner.
  • The checkout method in LibraryMember uses self.class to dynamically call class methods (checkout_book and books_checked_out) on the Library class.

This design ensures shared data is managed centrally while allowing dynamic access and modifications through instance methods.

Lesson Summary

In this lesson, we explored inheritance in Ruby, covering both attribute and method inheritance. We also discussed how the super keyword allows subclasses to access superclass methods for extending or combining behaviors. Practicing inheritance will help you write more efficient, organized, and modular Ruby code. Get ready to apply these concepts in some hands-on exercises to deepen your understanding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal