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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
Extending Classes in Ruby
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.

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.speakUsing 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.speakOverriding 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.speakUnderstanding 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.speakKeeping 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.speakExtending Methods with Open Classes
Ruby allows reopening classes and modifying their methods dynamically.
class String
def shout
self.upcase + '!!!'
end
end
puts 'hello'.shoutFinal 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.moveCongratulations!
You have learned how to use inheritance and polymorphism in Ruby.
Next, you will explore mixins and modules.

Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Inheritance and Polymorphism” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Inheritance and Polymorphism”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- Classes and Objects
- Instance Variables and Methods
- Inheritance and Polymorphism
- Mixins and Modules