OOP Inheritance aka "It's About Family"
Eben Eleazer
Posted on August 14, 2022
I wish I had a rich uncle, but unfortunately we're not talking about that kind of inheritance.
We are talking about family, though.
Specifically, Inhertiance is talked about in terms of parent classes and child classes.
Super classes
How can one class be a parent of another? It's not like our classes are out here giving birth. In fact, some people prefer to call them "super" classes instead, and personally, I think that makes more sense.
So if a class consists of common properties and behaviors of other objects (or instances) then what do you think a super class would be?
Exactly, It has properties that are common to other classes!
When we say super, or parent, what we're actually saying is "more general".
All Squares are Rectangles
Let's look at an example that you're likely familiar with. I'm sure you remember from geometry what a rectangle and a square are.
You'll also likely remember that all squares are also rectangles, but all rectangles are not necessarily squares. A square has all the properties of a rectangle (4 sides, 4 right angles), but it also has additional properties (all sides are equal length).
If we make a class for rectangles:
class Rectangle
@sides = 4
@all_right_angles = true
end
and squares:
class Square
@sides = 4
@all_right_angles = true
@all_sides_equal = true
end
we'll see that all the things in the rectangle class are in the square class.
Since the whole point of a class is to be more abstract, and more general, the more general class is the "super" class. In this case the rectangle is the super or parent class. The square class would be considered a subclass or child class.
Grandparent Classes and other relations
A rectangle is the parent of a square, because a square has all the properties and behaviors of a rectangle.
But, aren't all rectangles also parallelograms? (If you forgot geometry, the answer is yes.)
And, aren't all parallelograms also quadrilaterals? And aren't all quadrilaterals also polygons? And aren't all... you get the idea.
Just like the parents of your parents, we can call these classes grandparent classes. Further relations can be considered decedents or ancestor classes, but it's not very common.
** Square < Rectangle < Parallelogram < Quadrilateral < Polygon **
There are also "sibling" classes that share the same parent but are otherwise unrelated.
A rhombus class for example, would be a sibling of the rectangle class, because they are both children of the parallelogram class!
Inheritance in Code
Now that we have the concept down, let's look at what it means for our coding.
look at our square and rectangle classes from before:
class Rectangle
@sides = 4
@all_right_angles = true
end
class Square
@sides = 4
@all_right_angles = true
@all_sides_equal = true
end
Since squares have all the properties and behaviors of rectangles, we can set up our square class as a child or subclass of rectangles. In ruby we do this using a '<' in the class declaration.
class Rectangle
@sides = 4
@all_right_angles = true
end
class Square < Rectangle
@all_sides_equal = true
end
Now the square class will * inherit * everything in the rectangle class, and we don't have to type it all out again!
Posted on August 14, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.