0Pricing
Ruby Academy · Lesson

Inheritance and Polymorphism

Explore how Ruby enables code reuse through class inheritance and method overriding.

1

Inheritance and Polymorphism

Inheritance allows a class to inherit behavior from another class.

Polymorphism allows different classes to respond to the same method in different ways.

In this lesson, you will learn how to use inheritance and polymorphism in Ruby.

Inheritance and Polymorphism — illustration 1

2

Using Inheritance

A subclass inherits behavior from a superclass using <.

class Animal
  def speak
    puts 'Animal makes a sound'
  end
end

class Dog < Animal
end

dog = Dog.new
dog.speak

All lessons in this course

  1. Classes and Objects
  2. Instance Variables and Methods
  3. Inheritance and Polymorphism
  4. Mixins and Modules
← Back to Ruby Academy