Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I was going through "Object Oriented Modelling and Design" by James Rumbaugh et al and it said that in languages where multiple inheritance is not supported like Java three mechanisms can be used as workarounds

  1. Delegation using aggregation of roles
  2. Inherit the most important
  3. class and delegate the rest Nested Generalization

I couldnt understand the examples given in the text. Could any one please explain this with an example. I know that "diamond problem" is a problem within multiple inheritance and java supports multiple inheritance of interfaces.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

My observation is that a lot of folk misuse multiple inheritance without working through composition type questions.

The "classic" OO example of a car leads to this problem. For example, we'll say that a car breaks down to the following parts:

  • it's a vehicle
  • there are tires
  • there is an engine
  • there is a chassis
  • there are seats
  • and so on...

so we'll define a class that looks like this:

 public class Car : Vehicle, Tires, Engine, Chassis, Seats {}

All well and good, but it presents some significant problems with encapsulation and constructors / destructors. And it doesn't accurately represent what a car looks like in the real world.

If we start asking composition questions then we'll see some nuances here that weren't at first obvious.

  • is a car a vehicle? (yes)
  • is a car a tire or set of tires? (no, it has a set of tires)
  • is a car an engine? (no)
  • is a car a seat? (no)
  • etc ...

So now our class can look something like this:

public class Car : Vehicle
   public Tires tires;  //ie, myCar _has_ a set of tires.
   protected Engine engine;
   private Chassis chassis;
   protected Seats seats;

Which is also all well and good. But now we start scratching our heads and say "you know, I really wish that when I instantiate a car that it has to have tires, an engine, and a chassis."

So now we can turn Tires, Engine, and Chassis into interfaces and we'll have the following class. I'm prefixing the interfaces with "I" to make the distinction more clear.

public class Car : Vehicle, ITires, IEngine, IChassis
   protected Seats seats;

Interfaces are like contracts. When a class implements an interface, it's guaranteeing it will provide certain methods and behaviors.

And now things really are all well and good. Our model of the car accurately reflects the real world representation of the same Object, including some guarantees about the resemblance between the two courtesy of the Interfaces. We have avoided the icky issues that multiple inheritance can create.

That's not to say that multiple inheritance is always bad. However, it's not really needed most of the time which is why languages like Java and C# don't allow for multiple inheritance.

share|improve this answer
I don't really see much advantage to multiple inheritance from the standpoint of having one type include code or fields from more than one other, but there can sometimes be major value in having a class be substitutable for multiple independent types. Interfaces in Java and .NET are useful for that, but they have some significant limitations. The compromise I'd like to see in a future OO framework would be to allow families of classes to inherit from other families, while retaining the relations among class members. Thus if one had family Foo with a readable abstract base class... –  supercat Dec 31 '14 at 5:48
...a read-only abstract base class, a read-only wrapper class, an immutable concrete class, and a mutable concrete class, it should be possible to derive a family Moo with similar classes, and have a mutable Moo be usable by code which expects either a mutable Foo or a readable Moo. Such a thing could be done with interfaces, but using interfaces rather than classes would result in a lot of boilerplate code. –  supercat Dec 31 '14 at 5:52

Java does not provide multiple inheritance. Interfaces are in fact not inherited they are implemented/realized. Inheritance is used to extend a class static and dynamic characteristics, with composition (delegation, aggregation) the behaviors of a class are extended by adding adding classes instances (attributes, references) it can use to perform the services it provides. For instance if you have a Class for a thread which needs a socket you can make the Thread class extending the Socket class (very bad decision) and use the method it inherits or make the Thread class owning a Socket object it can use and delegate to it the socket stuff. Same behavior very different architecture.

share|improve this answer
Technically you can inherit from multiple interfaces into one interface, but you correct in that you must implement methods from all interfaces in your class that implements that interface. –  Heath Lilley Aug 7 '12 at 15:02
yes I meant Java does not provide multiple inheritance fro classes :) –  Silli Aug 7 '12 at 15:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.