Three characteristics of object-oriented

Source: Internet
Author: User

The three main characteristics of object-oriented are encapsulation, inheritance and polymorphism.

Encapsulation : The so-called encapsulation is to encapsulate objective things into abstract classes, a set of data and operations of these data functions encapsulated together to form a class, in declaring the class, we have some data and functions declared to be public, some data and functions are declared private, which are designated as common data or functions, That is, it can be called by a member function in this class, or it can be called by a statement outside the class, a member designated as private can only be called by a member function of this class, not by a statement outside the class, which is intended to protect some data from the erroneous invocation of a private member by an unrelated part of the program.

Inheritance: Inheritance is mainly to solve the problem of reusability of software, so-called inheritance is to build a new class on the basis of an existing class, the new class from the existing class where to obtain its existing characteristics, the new class created by inheritance becomes a subclass or a derived class, the inherited class becomes the parent class or the base class.

polymorphism: The same information is received by different objects may produce a completely different behavior, which is polymorphism, polymorphic mechanism enables objects with different internal structures can share the same external interface, polymorphism is divided into static polymorphism and dynamic polymorphism, function overloading is static polymorphism, Static polymorphism knows the full information of the calling function at program compile time, when the program compiles it can decide which function to call, and the other is dynamic polymorphism, the dynamic polymorphism is realized by virtual function, when we use the pointer of the base class object to access the derived class, when we are not using the virtual function. We can only access the base class members in the derived class, not the members added by the derived class, when a member function of our base class is declared as a virtual function, allows the function to be redefined in a derived class, gives it new functionality, and can invoke a function of the derived class by pointing to a pointer to the base class. The dynamic polymorphism implemented by virtual function is the same kind of object in the same class, and responds differently to the same function call.

What is the role of polymorphism, encapsulation can hide implementation details, so that code modularity, inheritance can be extended to exist code modules, their purpose is for code reuse, and polymorphism is to achieve another purpose-interface reuse,

Three characteristics 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.