Main Content

The Light bulb moment when people see how Object Orientation works in real use

Archive - Originally posted on "The Horse's Mouth" - 2010-05-28 07:55:12 - Graham Ellis

A big "light bulb" moment for many customers is when they "get" object inheritance. And one of the best ways of explaining it is by writing an example.

On Wednesday, I wrote an example during our Python Course - taking a customer of Well House Consultants as my base class, then defining two extended classes (i.e. subtypes with extra and amended features) called "resident" and "delegate". See [source].

In my example, all customers have names and a length of stay, but the price algorithm differs in the subclasses. I've also redefined the "+" operator, so that if I add two residents together they end up sharing a room, and if I add a number to a resident, that extends their stay by that number of nights.

When I call up the name of a customer, it runs the same code for any customer, but when I call up the price for a customer, it runs different code depending on whether it's a delegate or a resident. This is "Polymorphism"

Python is very neat in that I can define all of the classes in a single file (A Java example would have had three or 4 files of source), and it's much cleaner than objects in Perl. Which is why Python is such a superb language for researchers.