Home > Common Problem > body text

What are the three elements of object-oriented programming?

烟雨青岚
Release: 2020-07-10 10:28:38
Original
17031 people have browsed it

The three elements of object-oriented programming are encapsulation, inheritance, and polymorphism. Encapsulation means that things are abstracted into classes, exposing external interfaces and hiding implementation and internal data; inheritance means that all functions of existing classes can be used; polymorphism means that a parent object is allowed to be set equal to one or more child objects Technology.

What are the three elements of object-oriented programming?

The three elements of object-oriented programming are encapsulation, inheritance, and polymorphism

The five basic elements of object-oriented programming Principles: Single Responsibility Principle, Open and Closed Principle, Liskov Substitution Principle, Dependency Inversion Principle, Interface Abstraction Principle

Encapsulation

Encapsulation is to abstract things into classes and external interfaces Expose, hides implementation and internal data.

Inheritance

One of the main features of object-oriented programming (OOP) languages ​​is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and extend it without having to rewrite the original class.

New classes created through inheritance are called "subclasses" or "derived classes".

The inherited class is called the "base class", "parent class" or "super class".

The process of inheritance is the process from general to special.

To achieve inheritance, you can achieve it through "Inheritance" and "Composition".

In some OOP languages, a subclass can inherit multiple base classes. But in general, a subclass can only have one base class. To achieve multiple inheritance, it can be achieved through multi-level inheritance.

There are three types of implementation methods for the inheritance concept: implementation inheritance, interface inheritance and visual inheritance.

Implementation inheritance refers to the ability to use the properties and methods of the base class without additional coding;

Interface inheritance refers to the ability to use only the names of properties and methods, but subclasses must provide implementations ;

Visual inheritance refers to the ability of a subform (class) to use the appearance and implementation code of the base form (class).

When considering using inheritance, one thing to note is that the relationship between two classes should be a "belongs to" relationship. For example, Employee is a person and Manager is also a person, so both classes can inherit the Person class. But the Leg class cannot inherit the Person class because the leg is not a person.

Abstract classes only define general properties and methods that will be created by subclasses. When creating abstract classes, use the keyword Interface instead of Class.

OO development paradigm is roughly as follows: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and synthesis) → using classes and instances to design and implement several stages.

Polymorphism

Polymorphism (polymorphisn) is a technique that allows you to set a parent object to be equal to one or more of its child objects, after assignment , the parent object can operate in different ways based on the characteristics of the child object currently assigned to it. To put it simply, it is one sentence: It is allowed to assign a pointer of a subclass type to a pointer of a parent class type.

There are two ways to achieve polymorphism, overwriting and overloading.

Overwriting refers to the practice of subclasses redefining virtual functions of parent classes.

Overloading means that multiple functions with the same name are allowed to exist, but the parameter lists of these functions are different (perhaps the number of parameters is different, the parameter types are different, or both are different).

In fact, the concept of overloading does not belong to "object-oriented programming". The implementation of overloading is: the compiler modifies the names of functions with the same name based on different parameter lists of the functions, and then these functions with the same name become different functions (at least as far as the compiler is concerned).

For example, there are two functions with the same name: function func(p:integer):integer; and function func(p:string):integer;. Then the function name modified by the compiler may be like this: int_func, str_func.

The calls to these two functions have been determined between the compilers and are static (remember: they are static). In other words, their addresses are bound at compile time (early binding), therefore, overloading has nothing to do with polymorphism! What is really related to polymorphism is "coverage".

When a subclass redefines the virtual function of the parent class, the parent class pointer dynamically (remember: it is dynamic!) calls the function belonging to the subclass according to the different subclass pointers assigned to it. , such a function call cannot be determined during compilation (the address of the virtual function of the called subclass cannot be given).

Therefore, the address of such a function is bound at runtime (late binding). The conclusion is: overloading is just a language feature and has nothing to do with polymorphism or object-oriented! To quote Bruce Eckel: "Don't be stupid, if it's not late binding, it's not polymorphic."

So, what is the role of polymorphism? We know that encapsulation can hide implementation details and make the code modular; inheritance can extend existing code modules (classes);

their purpose is-code reuse. Polymorphism is to achieve another purpose-interface reuse! The function of polymorphism is to ensure the correct call when using a certain attribute of an instance of any class in the "family tree" when the class inherits and derives.

For more related knowledge, please visit PHP Chinese website! !

The above is the detailed content of What are the three elements of object-oriented programming?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template