Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Mark Leighton Fisher (4252)

Mark Leighton Fisher
  (email not shown publicly)

I am a Systems Engineer at Regenstrief Institute []. I also own Fisher's Creek Consulting [].
Friday May 23, 2008
12:04 PM

Good Inheritance and Bad Inheritance

[ #36502 ]

Inheritance is evil, and must be destroyed is the slightly overwrought title of an article by BernieCode that, nonetheless, expresses an idea that I've long held that most use of inheritance is better represented by either composition (HAS-A rather than IS-A) or by interface implementation/Perl 6 roles (ACT-AS rather than IS-A).

Inheritance works well for classes that are actually closely related (the canonical example of classes that represent the relationship of various species springs to mind here). What you often want (in my experience) are classes that can act in a certain way for example, a horse and a dog that can act like a pet. The EventManager example in the article above is a particularly good example of where a Perl 6 role/Java interface/etc. solves a problem much more neatly and clearly than inheritance does.

By the way, Solving compositional problems with Perl 6 roles (which I just discovered) also looks like a pretty good resource on this topic, especially for us Perl users.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Moose makes all these very easy. First of all it supports classic inheritance, but clarifies the isa chain and adds a default base object (Moose::Object) that everything inherits from. You can also declare Roles and use attributes (Perl6 style attributes, not method/variable attributes) to compose functionality. I highly recommend it.
    Waiting on the Road to Eventually, I lost my Place On Line