Three basic features and five basic principles of object-oriented

Source: Internet
Author: User
Three main features are:
  • Encapsulation
  • Inheritance
  • Polymorphism
Five basic principles:
  • Single Responsibility Principle SRP (single responsibility principle)
  • OCP (Open-Close principle)
  • The liskov substitution principle LSP)
  • The dependency inversion principle dip)
  • The interface segregation principle ISP)
Details:

The so-called encapsulation refers to encapsulating objective things into abstract classes, and classes can only allow trusted class or object operations for their own data and methods to hide untrusted information. Simply put, a class is a logical entity that encapsulates data and the code that operates the data. In an object, some code or data can be private and cannot be accessed by the outside world. In this way, the object provides different levels of protection for internal data to prevent unexpected changes or incorrect use of the private part of the object in unrelated parts of the program.

Inheritance refers to a method that allows an object of a certain type to obtain the attributes of an object of another type. It supports the concept of classification by level. Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class. The new class created by inheritance is called "subclass" or "derived class", and the inherited class is called "base class", "parent class", or "superclass ". The process of inheritance is from general to special. To implement inheritance, You can implement it through "inheritance" and "Composition. There are two methods to implement the concept of inheritance: Implementation inheritance and interface inheritance. Implementation Inheritance refers to the ability to directly use the attributes and methods of the base class without additional encoding; interface inheritance refers to the ability to only use the names of attributes and methods, but the subclass must provide implementation;

Polymorphism means that the same method of a class instance has different forms in different situations. Polymorphism allows objects with different internal structures to share the same external interface. This means that although the specific operations on different objects are different, they can be called in the same way through a public class.

Single Responsibility Principle SRP (single responsibility principle)
It means that a class has a single function and cannot be all-encompassing. Like a person, you cannot allocate too much work. Otherwise, even though you are busy all day long, the efficiency is not high.

OCP (Open-Close principle)
A module should be open in terms of scalability and closed in terms of modification. For example, a network module, originally only serves the server, but now needs to add the client function,
Therefore, the client function implementation code should be added without modifying the server function code. This requires that the server and the client be separated and the public part be abstracted at the beginning of the design.

The liskov substitution principle LSP)
Subclass should be able to replace the parent class and appear anywhere the parent class can appear. For example, if an annual gala is held in a company, all employees can participate in the lucky draw,
Employees at the headquarters or expatriates should be able to participate in the lucky draw. Otherwise, the company will not be harmonious.

The dependency inversion principle dip depends on the specific abstraction, and the upper layer depends on the lower layer. Assume that B is a lower module than a, but B needs to use the function of,
At this time, B should not directly use the specific class in a: instead, B should define an abstract interface and a will implement this abstract interface. B only uses this abstract interface: in this way
B also removes the dependency on A. In turn, a depends on the abstract interface defined by B. It is difficult to avoid dependency on lower-layer modules through upper-layer modules. If B is also directly dependent on the Implementation of A, it may cause circular dependency. A common problem is that when compiling module A, it needs to directly include the CPP file of Module B. When compiling module B, it also needs to directly include the CPP file of module.

The interface segregation principle ISP)
Modules must be isolated from each other through abstract interfaces, instead of being strongly coupled with specific classes.

Three basic features and five basic principles of object-oriented

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.