Inheritance & super

Inheritance is the object-oriented mechanism that lets one Ruby class reuse and extend another, written class Child < Parent , so shared behaviour lives in one place instead of being copied everywhere.

Learn Inheritance & super in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll override methods, reach back into a parent with super , check types with is_a? , and control access with protected and private .

What You'll Learn in This Lesson

1️⃣ Subclassing & Method Overriding

Write class Dog < Animal and Dog instantly gains every method Animal defines. When you define a method with the same name in the child, it overrides the parent's version for that class only — the parent and its other children are untouched.

2️⃣ Reusing the Parent with super

Overriding often means "do what the parent does, and then a bit more." That's what super is for: it calls the parent's version of the current method. Use super(a, b) to pass specific arguments, bare super to forward the same arguments you received, and super() to call the parent with none.

3️⃣ Types, Modules & Visibility

Use is_a? (alias kind_of? ) to ask whether an object descends from a class or includes a module; use instance_of? for an exact-class check. Ruby allows only one parent class, but you can include any number of modules to share behaviour across unrelated classes.

Visibility controls who may call a method. A private method can only be called without an explicit receiver; a protected method can be called with a receiver as long as the caller is the same class — perfect for comparing two objects' internals.

🎯 Your Turn

A Motorcycle is a Vehicle with two wheels. Replace each ___ with super so the subclass reuses its parent, then run it.

Build a three-level hierarchy: Square < Rectangle < Shape . Each level uses super to reuse its parent's setup, and a Square should report as both a Rectangle and a Shape . Run with ruby shapes.rb .

📋 Quick Reference — Inheritance

Practice quiz

How do you declare that Dog inherits from Animal?

  • class Dog : Animal
  • class Dog extends Animal
  • class Dog < Animal
  • class Dog from Animal

Answer: class Dog < Animal. Ruby uses the < operator for single inheritance.

When a subclass defines a method with the same name as its parent, the method is:

  • overridden for that subclass
  • deleted from the parent
  • run twice
  • a syntax error

Answer: overridden for that subclass. Overriding takes priority for the subclass; the parent's version is untouched.

What does bare super (no parentheses) pass to the parent method?

  • no arguments
  • only the first argument
  • a copy of self
  • the same arguments the current method received

Answer: the same arguments the current method received. Bare super forwards all of the current method's arguments.

What does super() with empty parentheses do?

  • forwards all current arguments
  • calls the parent method with no arguments
  • is a syntax error
  • calls the child method again

Answer: calls the parent method with no arguments. Empty parentheses call the parent explicitly with zero arguments.

Given class Dog < Animal, what does Dog.new.is_a?(Animal) return?

  • true
  • false
  • nil
  • an error

Answer: true. is_a? is true for any ancestor class or included module.

What does dog.instance_of?(Animal) return when dog is a Dog?

  • true
  • nil
  • false
  • raises NoMethodError

Answer: false. instance_of? checks the EXACT class only, so a Dog is not an Animal.

How many direct superclasses can a Ruby class have?

  • zero
  • exactly one
  • exactly two
  • unlimited

Answer: exactly one. Ruby has single inheritance; share extra behaviour with module mixins.

What is kind_of? in relation to is_a??

  • its opposite
  • an exact-class check
  • deprecated
  • an alias of is_a?

Answer: an alias of is_a?. kind_of? and is_a? are aliases that behave identically.

A protected method can be called with an explicit receiver when:

  • never
  • the caller is the same class or a subclass
  • always
  • only from outside the class

Answer: the caller is the same class or a subclass. protected allows same-class collaboration like a.richer_than?(b).

How do you add a module's shared behaviour to a class?

  • import Swimmer
  • inherit Swimmer
  • include Swimmer
  • use Swimmer

Answer: include Swimmer. include mixes a module's instance methods into the class.