Encapsulation Inheritance polymorphism What the hell is that about?

Source: Internet
Author: User

Encapsulation, inheritance, polymorphism is not proposed for the C # language, he is a concept under the object-oriented thinking. So to understand encapsulation, inheritance, polymorphism, you first need to understand the object-oriented concept.

Encapsulation: When we refer to object-oriented, how does this object come? Is through our man-made encapsulation. Encapsulation is the combination of features or functions onto an abstract object. Like a computer: It features a display, a host, a mouse and keyboard, and so on. Functions are calculated and so on. When we combine these features and functions into the abstract noun "computer", the "computer" has these features and functions. But this "computer" is not a physical object, it represents the name of all the objects called "Computer". In C #, we call the encapsulated abstract noun "class", so we call the "computer" a class, and the physical computer is called the object (or instance) of the class. The inheritance and polymorphism are the characteristics of the class.
Inheritance: This word is interpreted with normal semantics, for example, you inherit the merits of your father. And in C # class can also inherit (single inheritance), for example, we have a "computer" this class, now we define a class called "Lenovo Computer", we have to define the characteristics of the computer, function again? can be possible, but the code is a bit repetitive, and the other direction is not convenient to manage and explain the characteristics of "Lenovo computer". So we can let "Lenovo computer" inherit from "Computer" This class, we call "Lenovo computer" as "computer" subclass or derived class, and "computer" is called the parent class or the base class. So, as long as "computer" something, "Lenovo Computer" has, but "Lenovo computer" can also evolve (add) their own unique things. Therefore, the parent class (the base class) is a subset of subclasses (derived classes).

polymorphism (polymorphism): is a technique that allows you to set a parent object to be equal to one or more of his child objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.

To achieve polymorphism, there are two ways, covering, overloading.

    • Override, which is the practice of redefining the virtual function of a parent class by a subclass.
    • Overloading (overload) means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps with different number of arguments, perhaps different types of arguments, or both).

In fact, the concept of overloading is not "object-oriented programming", the implementation of overloading is: The compiler according to the function of a different parameter table, the name of the same name is decorated, and then the same name function is a different function (at least for the compiler).

For example, there are two functions of the same name: function func (p:integer): Integer, and function func (p:string): integer; Then the compiler has done a modified function name may be this: Int_func, Str_func. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound (early bound) at compile time, so overloading and polymorphism are irrelevant ! The true and polymorphic correlation is "overlay". When a subclass has redefined the virtual function of the parent class, the parent pointer is dynamically based on the different child-class pointers assigned to it (remember: it's dynamic!). Call to the function of the subclass, such that the function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given). Therefore, such a function address is bound at run time (late binding). The conclusion is that overloading is only a linguistic feature, independent of polymorphism, and irrelevant to object-oriented! quoting the words of Bruce Eckel: "Don't be silly, 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 code modular; Inheritance can extend existing code modules (classes); they are all designed to- code reuse . And polymorphism is for another purpose-- interface Reuse ! The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.

Note: (This article from the more than reprint 1.:http://www.nowamagic.net/librarys/veda/detail/1835)
( This article comes from the multi-reprint 2.:http://zhidao.baidu.com/link?url=2iruvrzvq_ifgj3vkc7jerlabk_8d 35QZFPLUJT5JEOCLHH-VFGU-MTXE3BUMU7Y2BTYHQFMIKMAPQO0HFJG9LZT9WMKR5ECH1GH4VY14CI)

Encapsulation Inheritance polymorphism What the hell is that about?

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.