Why Ali Baba advises developers to use caution inheritance?

Many people know that bloggers recently updated a topic - "Interpretation of Alibaba Java Development Manual", this is the topic of another article.

Java to learn from the very first day, we know that Java is an object-oriented language, Java and learning the next day, we know the three basic object-oriented features are: encapsulation, inheritance, polymorphism.

So, for many developers, are certainly no stranger to inherit. However, for certain inherited all the scenes it? There is no taboo to use inheritance to do code extension really okay?

Why "Ali Baba Java Development Manual" contains a provision: prudent use of inheritance way extend preferential use in combination to achieve.

![][1]

In this paper, to solve these problems, a simple analysis.

Object-Oriented Reuse

Everyone at the time just learning inheritance will more or less have such an impression: inheritance can help me achieve multiplexing class. Therefore, many developers when needed reuse some code will be very natural way to use class inheritance, because the book is so written (so the teacher is teaching). But, in fact, doing so is wrong. Long-term heavy use of the code will inherit bring high maintenance costs.

The aforementioned multiplex, here is a brief introduction to object-oriented multiplexing.

Reusability is one of the great potential benefits of object-oriented technology brings. If used well, it would help us save a lot of development time, improve development efficiency. However, if abused then it may generate a lot of code difficult to maintain.

As an object-oriented development language, the Java code reuse is one interesting feature. Reuse Java code inheritance, as well as a combination of three specific proxy forms.

inherit

Inheritance (Inheritance) hierarchical model of linking classes and class. It refers to a class (called a subclass, sub-interface) inherit another class (called the parent, the parent Interface) functions, and can increase the capacity of its own new features, inheritance is a class with a class or interface the most common relationship between interfaces.

![][2]

Inheritance is a is-a relationship. Fruits such as apples, the dog is an animal, husky dogs.

combination

Combination (Composition) reflects a whole-part relationship have.

![][3]

Combination is the relationship of has-a. Such as a car engine, there is a school teacher and so on.

Combined with the inherited differences

First of all, from the point of determining the relationship between class time, a combination of inheritance and there is a difference:

Inheritance, when writing code which will inherit specific named class, so at compile determine the relationship. And to the inheritance from the base class can not be changed dynamically at runtime, thus reducing application flexibility.

Combination, when writing the code for programming interfaces may be employed. Therefore, the combination of relationship classes generally determined at runtime.

In addition, there are certain differences are code-multiplexed manner:

Inheritance structure, the internal details of the parent class for the subclass is visible. So we can say generally inherited through code reuse is a white cassette code reuse .

If the base class is changed, then the derived class implementation will also change. This leads to unpredictable behavior of the subclass.

New combination is generated, by performing more complex functions assembly (composition) of an existing object. Because between objects, their internal details are not visible, so we can say that in this way the code reuse is black cartridge code reuse .

Because the portfolio is generally defined a type, so do not know at compile specific methods which achieve class will be called.

Finally, Java does not support multiple inheritance, and the combination is not limited. Like a person can only have one father, but he can have a lot of cars.

Comparative advantages and disadvantages

Combination of relationship
Inheritance
Advantages: does not destroy the package, the overall loose coupling between classes and local classes, relatively independent of each other
Disadvantages: a tight coupling between the package destroyed, the child and parent class, subclass depends on the implementation of the parent class, subclass lack of independence
Advantages: better scalability
Disadvantages: support the expansion, but tend to increase the complexity of the structure of the system at the expense of
Pros: Supports dynamic combination. At runtime, the whole object can choose different types of partial objects Disadvantages: does not support dynamic inheritance. At run time, you can not select different subclasses of the parent class
Advantages: the whole class can be packaged, partially encapsulated local class interface class to provide a new interface Disadvantages: subclasses of the parent class can not change the interface
Disadvantages: the whole class can not automatically obtain the same class and the local interface
Advantages: subclass can automatically inherit the parent class interfaces
Disadvantages: When you create a whole class of objects, you need to create objects of all local classes
Pros: When you create a subclass of objects, without having to create an object of the parent class

Why a combination of superior inheritance

I believe many people know that there is an object-oriented a more important principle of "multi-purpose combination, less inheritance" "better than the combination of inheritance." Or. From the previous description has advantages and disadvantages comparison can be seen, the combination is indeed more flexible than inheritance, but also more conducive to code maintenance.

So, it is recommended at the same feasible, instead of using a combination of priority inheritance. Because the combination is safer, simpler, more flexible and more efficient.

Note that it does not mean that inheritance that are not used, and said earlier that under the same [feasible]. There are some scenes still need to use inheritance, or is more suitable for use inheritance.

In addition to "Ali Baba Java Development Manual", and in many other materials are also described and constraints on the use and combination of inheritance:

Inherited be used with caution, their use is limited to the occasion you are sure to use the technology effectively the situation. A determination method is to ask whether they need to be upward transition to a new class of the base class. If necessary, the succession is necessary. On the contrary it should carefully consider whether you need inheritance. "Java programming ideas"

Only when the subclass really is a subtype of the superclass of the time, only for inheritance. In other words, for the two classes A and B, only when the existence is-a relationship between the two, it should continue to Class B Class A. "Effective Java"

[1]: https://www.hollischuang.com/wp-content/uploads/2019/09/15672352331109.jpg[2]: https://www.hollischuang.com/wp-content/uploads/2019/09/15672382013120.jpg[3]: https://www.hollischuang.com/wp-content/uploads/2019/09/15672382895979.jpg

Guess you like

Origin juejin.im/post/5d75b099e51d4561e62371d9