Class Inheritance

Today, after returning from a weekend (largely) spent offline, I was catching up on some reading and ran across a column on entitled Class Inheritance with PHP. Let me start with this disclaimer: I know nothing of, its reputation or its editorial process, if any. Nonetheless, I was a little surprised by what I read.

Inheritance is a fundamental concept of OOP and, as the author correctly states, is one means of “simplifying and reducing instances of redundant code”. A vast oversimplification, I think, but not technically incorrect so I read on to this pseudo-example:

Consider a tree. A tree is made up of many parts, such as the roots that reside in the ground, the trunk, bark, branches, leaves, etc. Essentially inheritance is a connection between a child and its parent.

Reading that, I was stunned. I have to assume that this is an introductory article. First, because of its introductory subject matter and second, because it’s barely two pages long including example code. Is this really how folks new to OOP should be thinking about inheritance? I think not.

While inheritance is a connection between a child and its parent, what the author is actually depicting in the example is composition, not inheritance. The first clue to that should have been the use of the phrase “made up of many parts”. Inheritance represents an “is-a” relationship, yet the author is describing a “has-a” relationship. A more accurate representation, still using the tree metaphor, might state that there are many types of trees – oak, maple, evergreen, etc. Each of these types of tree share a good many attributes and behaviors, but differ in others. Even with these differences, an oak tree – and each of the others listed – is-a tree and can properly inherit shared attributes and behaviors from a generic tree.

I almost stopped there, but decided to read on. After all, there was nowhere to go but up, right? To be fair, the column did proceed in a generally upward direction, but I don’t think it clarified the concept of inheritance much. Not for OOP beginners, at least. The example focuses on a Car class that inherits from a Product class. Not inaccurate in that a car can certainly be a product, but surely there are better introductory examples that could have been used. Product is too generic and as a result the parent-child relationship defined is too ambiguous for a column introducing basic material. A better example, following the same metaphor, might have been to subclass Vehicle with Car, SportUtilityVehicle, MiniVan, etc. At least there’s a clear and commonly understood relationship between each class.

Even as introductory material – and maybe especially as introductory material – I’m surprised that this column saw the light of day. If is unmoderated and unedited, then so be it, but it doesn’t appear that this is the case. This column, in its current form, does not provide an accurate depiction of the concept of inheritance. It’s simply misleading.

Subscribe1 Comment on Class Inheritance

  1. Jack said...


    Man, your mind fascinates me. Thanks again for sharing.