liveBook · Manning

Inheritance

As explained in The Well-Grounded Rubyist, Fourth Edition

Class inheritance is the Ruby mechanism by which a class derives behavior and structure from another class.

Suppose every publication needs a publisher, but magazines also need an editor. Ruby lets Magazine inherit the shared behavior from Publication while defining its own. In `class Magazine < Publication`, `<` names Publication as Magazine’s superclass. A Magazine instance can call both the inherited `publisher` methods and its own `editor` methods. section 3.5

class Publication
  attr_accessor :publisher
end
class Magazine < Publication
  attr_accessor :editor
end

The two class definitions show the superclass relationship and the separate attributes contributed by each class. section 3.5

The chain can continue: `class Ezine < Magazine` gives Ezine instances methods from both Magazine and Publication, even if Ezine defines no new methods. More generally, an instance can use instance methods defined in its class or its ancestral classes. Ruby finds a matching method along a method-lookup path; that path can include modules as well as classes. section 3.5 section 4.2.1

Each Ruby class has only one superclass. That makes inheritance useful for sharing methods, but not a way to express every relationship with classes alone. Modules can supply shared behavior without occupying a class’s one superclass position. When choosing between them, consider whether you are modeling an entity that needs instances or a characteristic shared by several entities. section 3.5.1 section 4.4.1

Choosing a superclass or a mixin

A CargoHold can inherit stack operations from Stack, then define loading and unloading methods that use them. This works, but commits CargoHold’s sole superclass position to Stack. Mixing in a module containing the stack operations leaves that position available for another superclass. section 4.4.1

Inheritance can express an “is a” relationship: an Ezine is a Magazine, and `is_a?` can check whether an object belongs to a class in its ancestry. That family tree does not completely determine an object’s behavior. An individual object can gain a method, and adding an instance method to its class or an ancestor can change what it can do later. section 3.8

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains declaration syntax, inherited methods, ancestry, single inheritance, and method lookup; leaves fuller module treatment beyond the initial class hierarchy.
Examples
Shows Publication, Magazine, and Ezine sharing attributes, then contrasts a CargoHold subclass with a module-based design.
What it assumes
Assumes familiarity with classes, instances, and methods while teaching the superclass relationship through concrete declarations.
What you can do after
Build it from scratch with `<`, then evaluate whether a superclass or module better supplies shared behavior.
How it connects
Connects inheritance to modules, mixins, method lookup, and per-object methods.

Also explained in

Compare how 19 books explain inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.