« RedBarGreenBar is ShellEnvy | Main | Donald Rumsfeld on change »

Inheritence: I've got blisters on my fingers

Darren Hobbs: Inheritance Sucks!

What about polymorphism you cry?

Not me.

Thats what interface inheritance is for. If you need to be able to polymorphically pass your composed object to methods that expect your contained object, implement a common interface and make your methods accept that instead. Your code will be cleaner, more modular and (most importantly) less coupled.

Inheritence was always a bad term for what is really better called type unification or even for logic wonks, quantifiers. Gibberish aside, all we're trying to do is to enable a function to work on a broader range of types and interfaces are great for that. Got an operation that's common across a range of classes? Have them be the same type by using an interface.

Most code uses inheritance more often than object composition, and much of the time composition would have been the better option. I suspect the reason is as prosaic as the fact that object composition involves more typing.

Yep, in the Java case, 'extends' is the son of #include. The best you can say about 'extends' is that it saves you keystrokes. It adds no power to a programming language. if anything it seems to make managing and changing programs more difficult, unless you're using it in a very controlled manner (ie, not like java.io.*).

Inheritence as macro inclusion:

class A {
void foo(){println("foo");}

class B extends A {
void bar(){println("bar");}


interface A {
void foo();

class X implements A {
void foo(){println("foo");}

class B implements A {
void foo(){new X.foo();}
void bar(){println("bar");}

I would prefer the second. I suspect it's more flexible.

March 25, 2003 11:37 PM


Trackback Pings

TrackBack URL for this entry: