Lesson 3
Inheritance in Ruby
Inheritance in Ruby

Welcome back! Now that you have a solid understanding of classes and objects in Ruby, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).

Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.

What You'll Learn

In this lesson, you'll understand how to use inheritance in Ruby. We'll cover:

  1. What Inheritance Is
  2. How to Implement Inheritance in Ruby
  3. Why Inheritance Is Beneficial
What Inheritance Is

Inheritance is a way to establish a relationship between a new class (subclass) and an existing class (superclass). The subclass inherits properties and behaviors (methods) from the superclass.

Here’s a simple example:

Ruby
1# Define the superclass Person with name and age attributes 2class Person 3 # Constructor to initialize name and age 4 def initialize(name, age) 5 @name = name 6 @age = age 7 end 8 9 # Display method to show name and age 10 def display 11 puts "Name: #{@name}, Age: #{@age}" 12 end 13 14 # Greet method to display a greeting message 15 def greet 16 puts "Hello, there!" 17 end 18end 19 20# Define the subclass Student, inheriting from Person 21class Student < Person 22 # Constructor to initialize name, age of the superclass, and the major of the student 23 def initialize(name, age, major) 24 super(name, age) 25 @major = major 26 end 27 28 # Display method to show name, age using the superclass's display method, and major of the student 29 def display 30 greet 31 super 32 puts "Major: #{@major}" 33 end 34end 35 36# Create a Student object and display its details 37student = Student.new("Bob", 25, "Computer Science") 38student.display 39student.greet

In this snippet, the Student class inherits from the Person class. It reuses the @name and @age attributes and methods from the Person class and adds a new attribute @major and a new display method to show the student's major.

When you declare a subclass in Ruby, you specify the class it inherits from using the < symbol. The subclass can then extend or override the functionality of the superclass.

In our example:

  • The Person class is the superclass.
  • The Student class is the subclass, inheriting @name and @age from the Person class.
  • The Student class also adds a new member, @major, and overrides the display method to include information about the major.
    • Notice how Student#display calls super to reuse the superclass functionality before adding its own details.
    • The greet method is also called from the display method to show how the subclass can access superclass methods.
  • Pay attention, that the student object can call the greet method, which is defined in the superclass.

It is important to note that the super keyword in Ruby is used to call a method in the superclass with the same name as the current method:

  • Without Arguments: When called without parentheses (super), it automatically forwards all arguments from the subclass method to the superclass method. This is convenient when the arguments in the subclass and superclass methods are identical, reducing redundancy.
  • With Arguments: When called with explicit arguments (super(arg1, arg2)), only the specified arguments are passed to the superclass method. This provides greater control and is useful when the arguments in the subclass differ from those in the superclass or require modification before passing.

In this specific case, using super with or without arguments would have the same effect because the arguments match.

Why It Matters

Inheritance is powerful for several reasons:

  1. Code Reusability: Instead of rewriting common functionalities, you can inherit them from a superclass, making maintenance easier and reducing errors.
  2. Extension: You can extend existing code by adding new features to a subclass without changing the existing superclass.
  3. Hierarchy: It helps in organizing code in a hierarchical manner, which reflects real-world relationships and improves code readability and structure.

Inheritance is a cornerstone of OOP, and understanding it will enable you to design more flexible and scalable applications. It's an essential concept for mastering OOP.

Excited to start practicing? Let's move on and put this theory into action!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.