Chapter 9. Classes
Finally, I’ll go into classes in some detail. By now you certainly know that Ruby is an object-oriented programming (OOP) language, and that the centerpiece of OOP is the class. A class is a container of sorts that holds methods and properties such as variables and constants (collectively known as class members). One of the most important things about classes is that they can be reused by means of inheritance.
A class can inherit or derive characteristics from another class. That means that a child
class or subclass can inherit the methods and data from a parent class. This parent is also
referred to as the superclass. This parent-child chain forms a hierarchy
of classes, with the base class at the root, top, or base of the
hierarchy. For Ruby, the base class of the hierarchy is Object.
Ruby supports single inheritance, like Java, not multiple inheritance, like C++. What’s the difference? With single inheritance, a class can only inherit from one other class; with multiple inheritance, a class can inherit from more than one class. The problem with multiple inheritance is that it opens up management issues such as name collision—that is, when classes and methods and variables have the same names but different meanings. Most programmers agree that single inheritance is less prone to headaches and avoids collision and other issues related to multiple inheritance; on the other hand, they would also like the power of multiple inheritance.
Java addresses the multiple-inheritance ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access