Inherited behaviour
When classifying objects, it's fairly common to start out with general classes and then delve in to create sub-classes that are more specialized. Think about it:
- Integers and Floats are both Numbers.
- A square is also a rectangle, and both in turn are quadrilaterals.
- Both cars and motorcycles are vehicles (but a motorcycle isn't a car).
This kind of parent-child relationship between classes is often referred to as inheritance, where the specialized class inherits the abilities of its more generic parent.
So according to Object#is_a?
, the object 1.0
is both a Float
and a Numeric
(the class that represents a number in Ruby).
This is a perfect example of sub-classes - here Float
is a sub-class of Numeric
, so 1.0
which is a Float
is also a Numeric
.
Does this mean that in Ruby, 1.0
is an instance of both the Float
class and the Numeric
class? Let's see for ourselves.
Alright, obviously 1.0
is an instance of Float
. So how does Numeric
come into the picture?
Or to put it another way: