3.5. Inheritance and the Ruby class hierarchy
Inheritance is a kind of downward-chaining relationship between two classes (the superclass and the subclass), whereby one class “inherits” from another and the instances of the subclass acquire the behaviors—the methods—defined in the superclass.
In this example, Magazine inherits from Publication. Note the syntax in Magazine’s class definition:
class Publication attr_accessor :publisher end class Magazine < Publication attr_accessor :editor end
The symbol < designates Magazine as a subclass of Publication. Because every publication object has publisher and publisher= methods (thanks to attr_accessor :publisher), every magazine object has those methods too. In addition, magazine objects have editor ...
Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.