Inheritance and combination of Java

Object-Oriented Reuse

 

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. This article will focus inherit multiplexing and multiplexing.

inherit

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

combination

Is a combination of the whole-part, we have a relationship, i.e., a has-a relationship.

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

 

how to choose

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.

Therefore, 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.

Guess you like

Origin www.cnblogs.com/lujiahua/p/11408024.html