Tuesday, November 24, 2009

Interfaced Vs. Multiple Inheritance

What happens when a class has a bad implementation of a method and you want to improve it? Well, you open your source code and start modifying it, but alas, you don't have its source!

It could be good to provide a better implementation of that bad method via inheritance, because you don't need to modify original source code, but in languages like Java, only one inheritance is possible. You can have multiple interfaces, but these don't provide implementations.

I think compilers should accept every method and only complain about duplicated methods or properties. For example, when you extend from two parents, compiler should stop and complain about two parents with the same method, so YOU can correct that inconsistency. Sometimes multiple inheritance can be used without problems, and in those special cases of method collisions, you need to work.

In cases like this, we need to rely on those run-time alternatives of Delegation, Composition, or Aggregation. The price of Java Strong-Typing is that we need to be Over-Specific. I am recalling those old COBOL days...

No comments:

Post a Comment