problemswithinheritance (ThoughtStorms)

Context : ObjectOrientedProgramming

In summary the fallacies are that there's only one kind of inheritance, inheritance is essential to OOP and static inheritance is all that's ever needed.

This article, referenced by ManagAbility (is http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html) is) good as well, particularly on the fragile base-class problem. (Which isn't a matter of single / multiple inheritance)


Interfaces vs MultipleInheritance

Over on PythonBlackBoardArchitecture, BillSeitz says :

I think Java interfaces were designed primarily as a way to provide multiple inheritance, which Python supports directly. --BillSeitz

And I respond ...

Spoken like a true Pythonista :-) Yeah. I spent approximately from 2001 - 2003 thinking Interfaces were just a sorry excuse for MultipleInheritance.

But then I wrote SdiDesk in VisualBasic where you have to use interfaces as a way to provide any kind of inheritance-like polymorphism. And that was quite a good lesson that delegation could (sometimes) be a reasonable thing to do.

And then the ManageAbility article above really made something much clearer for me. It separated inheritance into several kinds.

The biggest win from inheritance in strongly-typed languages like Java and C++ isn't Reuse, it's PolyMorphism - the fact that you can later on pass new types of objects to old functions. In fact you can't do polymorphism without either inheritance or interfaces.

When I first started writing C++ I didn't really understand this, because I'd learned OO programming in SmallTalk which doesn't have types and therefore never had an issue with passing new kinds of thing to an existing situation. So I did lots of things badly in my first big C++ program, and ended up with a huge explosion of sub-classes and very inflexible and laborious to change code; everything was written to talk to other concrete implementations rather than abstract interfaces. And every time I changed something I'd have to chase a cascade of other changes in dependent code.

Now, I realize, I could have used abstract classes as interfaces to solve this problem. And if I was forced to write C++ again I'd definitely go this route.

Of course, I still think that PolyMorphism is better handled by not throwing it away at the beginning by compile-time type checking. (ie. the SmallTalk / PythonLanguage road)

But I can see if you come from a culture of C++ / Java that's a freedom you don't miss because you never had it, nor thought you ought to have it. It seems like the language is doing you a favour giving this capacity to pass new things to old functions. But given that's why you're using inheritance, there is a real danger of it getting mixed up with inheritance which is about code re-use.

In Python, you never implement inheritance just to get polymorphism; only when you want re-use. So you aren't likely to encounter the kinds of confusions that made C++ / Java want to distinguish the two.


Contrast :