The Power of Multiple Inheritance

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I find it sad that many .NET framework developers and Java developers chant
the mantra, "multiple inheritance is bad... multiple inheritance is bad..."
while happily implementing away multiple interfaces in their classes and
raving about the power of being able to apply combinational patterns to
composite multiple interfaces into a whole. This is a form of multiple
inheritance!

The pitfalls with multiple inheritance come when the base classes become too
complex. Sun had the right idea when they dumbed down multiple inheritance to
the point where only stateless, interfaces devoid of any logic could be
multiply inherited. The problem was that they dumbed it down too much.

Why exclude state variables, for instance? Take the interface for a text
control (ITextControl, if you like). The shared component of all classes that
implement this interface should be a text property, but no class would
implement this text property without an associated string variable storing
this state. Why require this redundancy from programmers that implement this
interface?

Further, I would bet that 99.9% of the classes that implement such an
interface will have the same redundant code for the read portion of the
property, and that will simply be to return the current state of the text
(just returning the value of the associated variable). Any programmer who
knows anything about good software engineering standards should know that
this kind of redundancy is the prime characteristic of poor, unreusable
design. The default, un-overridden behavior of such a text control base
should be to return the associated state variable when reading from the
property and to change this associated state variable and notify the host
(the class that derives the text control base) of this change. This default
behavior should satisfy the design of 99.9% of the classes that composite
this text control base into their design, with the only requirement for
working the text control into their design is that they implement the
callback for when the property is modified (this should usually just be a
matter of redrawing the control on the text change).

Now, take a look at the .NET framework library or the Java library and you
can see the unforeseen pain that resulted from dumbing down multiple
inheritance to nothing but interfaces. Look at the scrollbar GUI classes, for
instance (HScrollBar and VScrollBar in .NET). They have a Text property that
they derive from Control. Why do they have a Text property when it is
absolutely useless for a scroll bar? I'll tell you why. The developers of the
..NET framework ran into the same issues of having to deal with a lot of
redundant code by implementing multiple interfaces to compose new controls,
so they figured it would be far more manageable to create a real, gigantic
base class called Control that would encompass the needed base functionality
and state variables of all the controls whether they're scroll bars or text
boxes or whatever rather than separating these aspects into multiple control
interaces that could be multiply inherited as necessary. This means having a
bunch of unused functions, state variables, and properties in each control.
However, they decided they could hide up this obfuscation by putting in IDE
hints to filter them out from intellisense and the property editor.

I would agree that their solution was the best they could do under the
limitations of .NET. However, making an overly complex base class that has
unneeded functionality that is simply filtered out through the IDE is far
from an elegant design solution if you ask me. Aside from being wasteful
(though this waste is worse in principle than in reality, where it is
probably trivial on most of today's machines running on a gig or two of RAM),
it is horribly unmanageable compared to the elegant alternatives provided
through skilled use of multiple inheritance. I shudder to think of how the
code looks like for the Control base. Still, it's obvious that Microsoft had
the money and manpower to deal with this unnecessary complexity as the .NET
framework does work well, but not all of us have the resources to throw a lot
of money and manpower into an unmanageable project. There are some major
design limitations that accompany the lack of sophisticated multiple
inheritance, and it saddens me to think of how much more elegant the .NET
framework could have been had it not been for these language limitations.

- John
.



Relevant Pages

  • Re: Visual C++ wont autcomplete?
    ... is not the control position on a dialog a "property"? ... But in VS6, the properties were organized by IMPORTANCE ... and two decades of UI design and books on good UI ... and over again that modality in user interfaces was completely wrong, ...
    (microsoft.public.vc.mfc)
  • Re: [patch] PCI Express Enhanced Config Patch - 2.6.0-test11
    ... To the Intel guess working out the ACPI 3.0 interfaces. ... How do we find the Root Complex Register Block? ... How does ACPI describe multiple MMCONFIG spaces? ...
    (Linux-Kernel)
  • Re: operator overloading
    ... but not divide - anything a mathematician would call a ring, ... Multipliable, Ring extends Addable, Multipliable, Dividable, Field extends ... If you provide an interface for each operator, obviously applications can define their own composite interfaces, but there may be places where the standard library uses multiple operators on a single type, so there might end up being some "standard" composite interfaces while other combinations of operators are unrepresented. ...
    (comp.lang.java.programmer)
  • Re: Help Request: "Class Not Registered"
    ... appreciate that the 'basic rules' for how an ActiveX ... Control should behave with a client came from the orginal VB product, ... Interfaces and Interface Inheritance. ... some MFC one. ...
    (microsoft.public.vb.enterprise)
  • Re: why is multiple inheritance not implemented in java?
    ... Suppose that the framework I'm using gives me implementations of all those useful bits of functionality. ... Your Java author would use interfaces instead, and hide the implementations that combine the contracts from you altogether. ... Concrete implementations of JComponent will have the equivalent of multiple inheritance of default implementations for all three of those interfaces, much as they would if they inherited from three base classes. ...
    (comp.lang.java.programmer)