Three basic characteristics of object-oriented and five basic principles

Source: Internet
Author: User

Understanding the three basic characteristics of object-oriented is the basis of understanding the five basic principles of object-oriented.

Three major features are: encapsulation, inheritance, polymorphism

The so-called encapsulation, that is, to encapsulate objective things into abstract classes, and classes can be their own data and methods only let the trusted class or object operation, to the untrusted information hiding . Encapsulation is one of the object-oriented features and is the main feature of object and class concepts. Simply put, a class is a logical entity that encapsulates the data and the code that operates the data . Within an object, some code or some data can be private and cannot be accessed by the outside world. In this way, objects provide different levels of protection to internal data to prevent unintended changes to unrelated parts of the program or to use the private parts of the object incorrectly.

Inheritance refers to a method that allows an object of one type to obtain the properties of another type of object . It supports the concept of classification by class. Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class . A new class created through inheritance is called a subclass or derived class, and the inherited class is called the base class, parent class, or superclass. The process of inheritance is from the general to the special process. To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition). The concept of inheritance is implemented in two categories: implementation inheritance and interface inheritance. Implementation inheritance refers to the ability to directly use the properties and methods of the base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but subclasses must provide the implementation;

The so-called polymorphic means that the same method of a class instance has different manifestations in different situations. Polymorphic mechanisms enable objects with different internal structures to share the same external interface . This means that although specific actions are different for different objects, they can be called in the same way through a common class.

Five basic Principles
Single Duty principle SRP (Responsibility Principle)
Refers to the function of a class to be single, not all-encompassing. Like a person, the distribution of work can not be too much, otherwise busy all the way, but the efficiency is not high up.

Open Closure principle OCP (open-close Principle)
A module should be open in terms of extensibility and should be closed in terms of change. For example: A network module, the original service-only function, and now to join the client function,
Then, without modifying the service-side function code, you can increase the client function implementation code, which requires that at the beginning of the design, the server should be separated from the client, the public part of the abstraction.

Replacement principle (the Liskov Substitution Principle LSP)
Subclasses should be able to replace the parent class and appear anywhere the parent class can appear. For example: The company engaged in annual party, all employees can participate in the lottery, so whether the old staff or new employees,
Whether it is the headquarters staff or the outside staff, should be able to participate in the lottery, or the company will not be harmonious.

dependency principle (the Dependency inversion Principle DIP)Depending on the abstraction, the upper layer relies on the lower layer. Suppose B is a lower module, but B needs to use the function of a,
At this time, B should not directly use the specific class in a: Instead, a abstract interface should be defined by B, and a to implement this abstract interface, B only use this abstract interface: this will achieve
For the purpose of dependency inversion, B also relieves dependency on a, which in turn is an abstract interface dependent on the B definition. It is difficult to avoid relying on the lower module through the upper module, if B is also directly dependent on the implementation of a, then it may cause cyclic dependence. A common problem is that when compiling a module, you need to include the CPP file directly into the B module, while compiling B will also include the CPP file of a directly.

Interface separation principle (the Interface segregation Principle ISP)
The modules are separated by an abstract interface, rather than by a specific class-strong coupling.

Three basic characteristics of object-oriented and five basic principles

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: 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.