Relationship between combination and inheritance

Source: Internet
Author: User

(1) programming for interfaces is not implemented yet;

Class inheritance is a basic mechanism for extending application functions by reusing the parent class function. It can realize polymorphism through the inheritance of abstract classes. When inheritance is used correctly, all Classes exported from the abstract class share the interface of this abstract class. The subclass only adds or redefines the operation, but does not hide the operation of the parent class. All subclasses can respond to the request in the abstract class interface, the subclass type is the subtype of the abstract class.

Manipulating an object based on the APIS defined in an abstract class has the following two benefits:

1) customers do not need to know the specific types of objects they are using. They only need to have the interfaces they expect;

2) customers do not need to know what classes they use to implement the object. They only need to know the abstract class that defines the interface;

(2) prioritize the use of object combinations, rather than class inheritance;

Class inheritance is usually called white box reuse by generating subclass reuse. The internal details of the parent class are visible to the subclass, and the object combination requires that the object to be combined has a well-defined interface, because the internal details of objects are invisible, they are called Black Box multiplexing.

One disadvantage of class inheritance is that it is statically defined at the time of compilation and cannot be changed from the implementation of parent class inheritance at the runtime. In addition, the parent class defines the specific representation of some child classes, because inheritance reveals the implementation details of its parent class to the Child class, inheritance is often considered to destroy encapsulation. One solution is to inherit only the abstract class, abstract classes generally provide fewer implementations.

Object combinations are dynamically defined at runtime. Because objects can only be accessed through interfaces, they do not break through encapsulation. In addition, object implementations are written based on interfaces, therefore, the implementation dependency is relatively small. Object combinations play another role in system design, that is, the preferential use of object combinations helps to keep each class encapsulated, at the same time, the inheritance layers of classes and classes will maintain a small scale. In addition, there will be more objects in the design of object combinations, the system behavior depends on the relationship between objects rather than being defined in a class.

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.