ALTERNATE UNIVERSE DEV

Iteration Podcast

Inheritance At Its Core

Acquiring Behavior Through Inheritance

A weekly podcast about programming, development, and design through the lens of amazing books, chapter-by-chapter.

Sani Metz - Object-Oriented Design in Ruby

“Inheritance is, at its core, a mechanism for automatic message delegation. It defines a forwarding path for not-understood messages.”

Where to Use Inheritance

Objects that share a common parent -

The objects that you are modeling must truly have a generalization-specialization relationship.

Bicycle Touring Company - FastFeet

Bikes - Mountain and Road Bikes

Bikes have an overall size, a handlebar tape color, a tire size, and a chain type.

  • Only mountain bikes have shocks
  • Only Road bikes have handlebar tape

Create a new empty Bicycle Class

Let RoadBike > Bicycle

And MountainBike > Bicycle

When in doubt put less code in the parent, it’s easier to promote code later when you need a shared code.

“The general rule for refactoring into a new inheritance hierarchy is to arrange code so that you can promote abstractions rather than demote concretions.”

A superclass may have many subclasses, but each subclass is permitted only one superclass.

This family tree image is, however, a bit misleading. In many parts of the biological world, it’s common for descendants to have two ancestors.

It’s really useful to rails a NotImplementedError in the parent class in the methods that are required from their children, for example, default tire_size -

“Creating code that fails with reasonable error messages takes minor effort in the present but provides value forever.”

Initialize > Post Initialize to append any or overrode attributes from the parent initialize method.

Initialize + post_initialize

Closes with a mic drop - Initializing a new RecumbentBike is so DRY and painless!

“Inheritance solves the problem of related types that share a great deal of common behavior but differ across some dimension.”

“The best way to create an abstract superclass is by pushing code up from concrete subclasses.”

“When your problem is one of needing numerous specializations of a stable, common abstraction, inheritance can be an extremely low-cost solution.”

PICKS

  • JP: Machine Learning with JavaScript
  • John: Refactoring UI by Adam Wathan and Steve Schoger -
  • Hard Wired Internet - Ran a Cat 5 Cable from my router - SO WORTH IT.

Episode source