Relationship between combination and inheritance in Object-Oriented

Source: Internet
Author: User

In the object-oriented process, two common functions are class inheritance and combination (Object composition ).

Class inheritance allows you to define the implementation of a Class Based on the implementation of other classes. This type of reuse by generating sub-classes is usually called white box reuse (white-box reuse ). The term "white box" is relative visibility. In the inheritance mode, the internal details of the parent class are visible to the Child class.

New and more complex functions can be acquired by assembling or combining objects. Object combination requires that the combined object has a well-defined interface. This reuse style is called black-box reuse because the internal details of the object are invisible.

Advantages and disadvantages of class inheritance:

Class inheritance is statically defined during compilation and can be directly used. The programming language directly supports class inheritance. Class inheritance can easily change the implementation of reuse. When a subclass is used to redefine the implementation of the parent class, it also affects its inherited operations.

The disadvantage of class inheritance is that inheritance is defined at the compilation time, so the implementation of inheriting from the parent class cannot be changed at the runtime. Inheritance exposes the implementation details of its parent class to the subclass. Therefore, inheritance is often considered to be "corrupt encapsulation ". There is a strong dependency between the subclass and the parent class. Any changes in the implementation of the parent class will inevitably lead to changes in the subclass. If the inherited implementation is not suitable for solving the new problem, the parent class must be overwritten or replaced by other more suitable classes. This dependency limits flexibility and ultimately limits reuse.

. An available solution is to inherit only the abstract class, because the abstract class usually provides fewer implementations.

Object combinationIt is dynamically defined at runtime by obtaining references to other objects. The combination requires that objects comply with their interface conventions and require more careful interface definition. These interfaces do not prevent you from using an object with other objects. Objects can only be accessed through interfaces. As long as the types are consistent, one object can be used to replace another object at runtime. The implementation of objects is based on interfaces, therefore, there are few dependencies in implementation.

Object combinations give priority to system design. Using Object combinations helps you maintain that each class is encapsulated and concentrated on a single task. In this way, the Class and Class inheritance layers will remain relatively small and are unlikely to grow into an uncontrollable giant. On the other hand, the Design Based on Object combination has more objects (but fewer classes), and the system behavior will depend on the relationship between objects rather than being defined in a class. This is the second principle of object-oriented design:Object combination is preferred, rather than class inheritance.

Ideally, you should not create new components for reuse. You should be able to use only the object combination technology to obtain the functions you need by assembling existing components. But this is rarely the case, because the set of available components is not actually rich enough. The reuse of inheritance makes it easier to create new components than to assemble old ones. In this way, inheritance and object combination are often used together.

However, experience shows that designers tend to overuse the Reuse Technology inherited. However, the design that relies on Object combination technology has better reusability (or simplicity ). You will see that the design pattern uses Object combination technology again.

Advantages of combination over inheritance:

1. combination allows the problem to be divided into child components independent from each other. Each component can be developed by different developers.

2. combination has less coupling than inheritance. Inheritance forces the coupling between the inherited class and the inherited class, but the combination does not.

3. The combination will have less code redundancy, and inheritance will easily lead to redundancy. B inherits from A. In many cases, B only uses part A, and the unnecessary part is redundant. Combinations can completely avoid these situations.

4. The combination has better encapsulation. The combination is based on the public interface, and every component does not know its internal implementation details. Inheritance implements the internal implementation of the parent class.

Expose to subclass.

5. The combination is easier to change. Any component can be changed to minimize the impact on other components, as long as the public interface remains unchanged.


Disadvantages of combination: many small modules are available to implement functions.

Combination

Inheritance relationship

Advantage: No encapsulation is damaged. The overall class and local class are loosely coupled and independent from each other.

Disadvantage: destroys encapsulation. Child classes are closely coupled with parent classes. Child classes depend on the implementation of parent classes, and child classes lack independence.

Advantage: good scalability

Disadvantage: expansion is supported, but it is often at the cost of increasing the complexity of the system structure.

Advantage: dynamic combination is supported. During running, you can select different types of local objects for the entire object.

Disadvantage: Dynamic inheritance is not supported. Different parent classes cannot be selected for subclass during runtime.

Advantage: The overall class can be used to package Department classes, encapsulate local class interfaces, and provide new interfaces.

Disadvantage: The subclass cannot change the interface of the parent class.

Disadvantage: The overall class cannot automatically obtain the same interface as the Department class

Advantage: subclass can automatically inherit the interface of the parent class

Disadvantage: when creating an object of the overall class, you need to create objects of all local classes.

Advantage: when creating a subclass object, you do not need to create a parent class Object

 

I recently learned the design model and summarized all the important things. Come on !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.