C # encapsulation, inheritance, and polymorphism of the three features,

Source: Internet
Author: User

C # encapsulation, inheritance, and polymorphism of the three features,

I. encapsulation:

Encapsulation is the first step in implementing object-oriented programming. encapsulation is to aggregate data or functions in units (called classes ). Encapsulated objects are generally called abstract data types.

Encapsulation:
The significance of encapsulation is to protect or prevent code (data) from being accidentally damaged. In object-oriented programming, data is regarded as a central element and is closely integrated with functions that use it, thus protecting it from being accidentally modified by other functions.

Encapsulation provides an effective way to protect data from Accidental damages. Compared to defining data (implemented using domains) as public in a program, we can define them as private in many ways. Private data can be indirectly controlled in two ways. First, we use traditional storage and retrieval methods. The second method is property ).

Using attributes not only controls the validity of data access, but also provides flexible operation methods such as "read/write", "read-only", and "Write-only.

Access modifier:

Private: only the class can be accessed.
Protected: classes and derived classes can be accessed.
Internal: only classes in the same project can be accessed.
Protected Internal: A combination of Protected and Internal.
Public: full access.

Ii. Inheritance:

Inheritance mainly implements code reuse and saves development time.

1. Inheritance in C # complies with the following rules:

2. new Keyword

If the parent class declares a protected or public method without the friend modifier, The subclass also declares the method of the same name. You can use new to hide the methods in the parent class. (Not recommended)

3. base keywords

The base keyword is used to access the members of the base class from the derived class:

Iii. Polymorphism

1. Polymorphism: the same operation acts on different objects and can have different interpretations to produce different execution results. At runtime, you can call methods in the derived class by pointing to the base class pointer.

Polymorphism during compilation:

Polymorphism during compilation is implemented through overloading. For non-Virtual members, during compilation, the system determines the operation based on the passed parameters, returned types, and other information.

Runtime polymorphism:

The running polymorphism is the operation that is performed only when the system is running. In C #, The Runtime polymorphism is implemented by virtual members.

The polymorphism during compilation provides us with a fast running speed, while the polymorphism during runtime brings us a high degree of flexibility and abstraction.

2. Implementation of polymorphism:

3. override Keyword:

Override the virtual modifier in the parent class to implement polymorphism.

Related Article

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.