0Pricing
Ruby Academy · Lesson

Inheritance and Polymorphism

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

Inheritance and Polymorphism is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

3

Using Super

The super keyword calls a method from the superclass.

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

class Dog < Animal
  def speak
    super
    puts 'Dog barks'
  end
end

dog = Dog.new
dog.speak

4

Overriding Methods

A subclass can override a method from the superclass.

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

class Dog < Animal
  def speak
    puts 'Dog barks'
  end
end

dog = Dog.new
dog.speak

5

Understanding Polymorphism

Different classes can implement the same method in different ways.

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

class Dog < Animal
  def speak
    puts 'Dog barks'
  end
end

class Cat < Animal
  def speak
    puts 'Cat meows'
  end
end

dog = Dog.new
dog.speak

cat = Cat.new
cat.speak

6

Keeping Superclass Methods

A subclass can extend a method instead of completely replacing it.

class Animal
  def speak
    puts 'Animal sound'
  end
end

class Bird < Animal
  def speak
    super
    puts 'Bird chirps'
  end
end

bird = Bird.new
bird.speak

7

Extending Methods with Open Classes

Ruby allows reopening classes and modifying their methods dynamically.

class String
  def shout
    self.upcase + '!!!'
  end
end

puts 'hello'.shout

8

9

Final Example of Overriding

Override methods to provide specific behavior for subclasses.

class Vehicle
  def move
    puts 'Vehicle is moving'
  end
end

class Car < Vehicle
  def move
    puts 'Car is driving'
  end
end

car = Car.new
car.move

10

Congratulations!

You have learned how to use inheritance and polymorphism in Ruby.

Next, you will explore mixins and modules.

Inheritance and Polymorphism — illustration 10

Frequently asked questions

Is the “Inheritance and Polymorphism” lesson free?

Yes — the full text of “Inheritance and Polymorphism” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Inheritance and Polymorphism”?

Explore how Ruby enables code reuse through class inheritance and method overriding. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inheritance and Polymorphism” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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