How is composition different from multiple inheritance?

In certain situations, the composition is protected by inheritance. I see this happening more and more in the Ruby and Javascript community.

Composition sounds like multiple inheritance. I even read this inside some Ruby implementations, the composition of the IS module is multiple inheritance with little syntactic sugar.

This is the same? If not, how is this different from multiple inheritance?

+5
source share
3 answers

It depends on what you mean by "multiple inheritance" and "composition." If composition and inheritance simply means adding to the list of messages to which the object responds, then they are by definition equal.

Let's say that classes are simply virtual method tables and that every object in the language is defined by a reference to the class, and some data. If the object responds to the message by calling the method search function associated with it by the class, and the method search function returns the method (if the class contains a method corresponding to the message), or recursively calls the method search function on it of the superclass, we have a language with a single inheritance and without composition. Multiple inheritance can be added in such a language by modifying the method's search function, as described in section 2.2 of Open Extensible Object Models , authored by Ian Puert. Basically, just adding a class that drops the method search to several other classes, not just one. It is easy to see that mixins / traits (I assume this is what you mean by composition) can be added in exactly the same way.

However, if by “composition” you mean that an object has other objects as instance variables, then there is good reason to use this instead of multiple inheritance: encapsulation. Some methods for objects in instance variables do not make sense if they can be called on the parent object.

+1
source

Inheritance and composition are similar to what they can achieve, and you allude to this when you say that composition

sounds like multiple inheritance

The fundamental difference can be illustrated in this code example:

Inheritance

class Fruit { ... } class Apple extends Fruit { ... } 

Composition

 class Fruit { ... } class Apple { this.fruit = new Fruit(); } 

The consequences of choosing a design should be obvious here:

Inheritance gives you the added benefit of creating functionality that is accessible to your subclasses, very easily updated, and helps create a logical hierarchical structure for your classes. However, superclasses are often considered volatile, as changing a superclass can have very far-reaching side effects that you will need to feel almost constantly.

Composition , on the other hand, adds an access level between subclasses and their parents. The advantage here is that changes in the “parent” class are far less likely to have far-reaching side effects. The trade-off here is that it increases complexity to some extent.

A good rule of thumb is to think about the semantics of your classes. Think about the relationship between the two classes. If you could say that child is parent , use inheritance; otherwise use the composition. In the above example, we can say that Apple is Fruit , so inheritance probably makes sense here. Again, this is an offer, not a rule!

Check out this article for a deeper explanation.

+3
source

Many people talk about multiple inheritance in a technical way, but I would like to talk about the design aspect.

In a good OOP (Object Oriented Programming) design, you should not use multiple inheritance. Why?

1.

Like GRASP templates, offer it with a high clutch pattern, the class does not have a lot of answers.

2.

Inheritance blocks your architecture. If you want to change something, you need to change everything. I will give you an example: imagine some classes like car , truck , boat and plane that inherit from the motorVehicle class. Now I am adding a new concept: car, truck, boat and plane are also moving. What should I do?

  • Use multiple inheritance? My class will have many answers (and hope my language allows).
  • Use composition for this new concept? But why is the car more motorVehicle than movingVehicle ?
  • Try mixing motorVehicle and movingVehicle . My class will have two different correlated answers, which is very bad.

What if I add a bike! That inheritance should only be used to factorize code for classes with the same responsibility!

Conclusion

If a class should have only one clearly identified responsibility, and you should only use inheritance for classes with the same responsibility, you should never use multiple inheritance in the right OOP design.

I suggest you always use composition with interfaces in order to have a low connection between your classes. This will give you more scalable, maintainable, and verifiable code!

In the previous example, using composition will cause some motor classes to inherit from the engine interface, some carriage classes inherited from the carriage interface, and car , truck , ... classes will “use” the car engine and carriage interface. And our bicycle can only "use" the moving interface!

+3
source

Source: https://habr.com/ru/post/1209572/


All Articles