Tuesday, September 16, 2003

Is extends evil?

Alan Holub argues that the extends keyword is evil. Is he right?

I think the root of the evil is not implementation inheritance per se but single inheritance. With non-diamond multiple inheritance, inherit specific interface implementations to make a compound object and an is-a relationship rather than use interface forwarding and a has-a relationship. This is more efficient and much easier to maintain: no forwarding code to write and test. Even better would be generic multiple inheritance (template base classes in C++) so that a consumer of the code can specify which implementation to inherit for a particular interface.

In contrast, single inheritance makes it very difficult as you must upfront decide which interfaces a superclass implements. Changes in that decision require changing the interfaces and implementations in both the superclass and subclass. This is a disaster for maintainability and comprehension.

No comments: