Sometimes our code examples don't make sense

May 27, 2016

I was reading some technical article the other day that started talking about class hierarchy and used the common setup
class Child < Parent
at which point I got distracted because it occurred to me for the first time--this example doesn't actually make sense! If you're using inheritance in (what I understand to be) the correct way, anyway. It doesn't make sense to subclass Child from Parent, because Child is not in fact a type of Parent. If your parent (ahem) class is going to be Parent, then really any subclasses should be things like SingleParent, or Mother, or AsianDad, or whatever else (and maybe these should all be modules anyway, to mix-in different behaviors...if different kinds of parents are all that different from each other in the first place anyway).

I guess we use the Child < Parent setup because we're thinking of class inheritance like a tree, which we can map in our heads to a family tree, and the "children" inherit some genetic material and behaviors from the parents. But maybe this should instead just be a class like Smith, from which you get many instances of different Smiths, and some instances of Smiths are related to other instances of Smith?

The different kinds of animals (Corgi < Dog) or sports examples might be more sensible generally.