Sponsored Link •
Bill Venners: Why did Java only allow multiple inheritance of interface? How should I apply
interfaces in Java designs? I didn't know, but I did soon realize that
although in five years of C++ I never once used multiple inheritance, I was finding Java's
special kind of multiple inheritance very useful. The process of struggling with Java's
interface changed the way I think about object-oriented design in general.
Were I to go back to C++ now, I would make much more use of ABCs that have no data
members and only pure virtual functions.
I'm curious if you've had any similar transformation regarding multiple inheritance. Is there anything you would say about ABCs or multiple inheritance and their place in designs now that you didn't say 10 years ago?
Scott Meyers: I don't have a lot that is new, but I would say two
things. First, if you look at
interfaces in Java or .NET, in addition to being
interfaces also have no data. I have come to appreciate that if you
use abstract base classes and eliminate any data from them, then a lot of the difficulties of
multiple inheritance that I wrote about just go away, even in C++.
I happen to think one of the contributions of Java was getting rid of some of the baggage
that comes with multiple inheritance in C++. I'm guessing, since I'm not a Java guy, that
the creators of Java looked at the multiple inheritance model of C++. They said, "This has
some good things, but it has some baggage too. So we're going to try to find a way to
have the good stuff and throw away the baggage." That's essentially what an
interface is. It is an abstract base class that doesn't have any of the things
that tend to give rise to trouble in C++, because it doesn't have any data. I think that's an
interesting idea. So I think
interfaces are an interesting idea.
Second, at the time in 1991 when I wrote that multiple inheritance guideline in Effective C++, a huge debate about multiple inheritance was raging in the C++ community. My advice to use multiple inheritance judiciously was based on two observations. First, at that particular time no one had really come out with a compelling example in which multiple inheritance was useful and there was no competing design that was at least as good. Second, even some of the people who had used multiple inheritance didn't have great things to say about it. Notably, Jerry Schwartz, who had implemented the IO streams library based on multiple inheritance, had publicly said that if he had it to do over again he would not use multiple inheritance. He would find some way to get rid of the multiple inheritance.
At the time I wrote that guideline in 1991 I had never seen a compelling example for the
use of multiple inheritance. I knew about the difficulties with multiple inheritance that I
have written about. At the time I was not talking about
they didn't exist in that name. And somebody like Jerry, who had a lot of experience with
multiple inheritance, was saying if he had to do it over again he would have found a way
to get rid of the multiple inheritance. That was the reason for my advice about multiple
inheritance at that time.
A certain amount of time has gone by. My feeling about multiple inheritance now is that
as long as you go into it with your eyes open, so you understand the kinds of things I
wrote about in the book, especially if you are using
that have no data, you will be able to side-step most of those difficulties. So I'm less
down on multiple inheritance now.
Frankly, though, my advice would not change. I would still say use multiple inheritance
judiciously, because if you have a choice between a single inheritance based design and a
multiple inheritance based design, I think most of the time the single inheritance based
design will be simpler. And I like simpler better than more complicated. But perhaps you
are faced with a multiple inheritance based design and a single inheritance based design,
and the multiple inheritance based design strikes you as being cleaner or in some other
way advantageous. As long as you've taken into account the difficulties with multiple
inheritance that have been talked about, probably you've eliminated them by using
interface classes, then I would say fine, follow your design instinct and
use the multiple inheritance. So my tone on multiple inheritance has softened a little bit.