Why C # has inheritance

Source: Internet
Author: User
Tags multiple inheritance in c

Encapsulation inheritance and polymorphism in C # are not related to C #. This is the definition in object-oriented programming.

The inheritance mechanism allows users to augment this class by adding, modifying, or replacing methods in the class to accommodate different application requirements. With inheritance, program developers can construct new classes on the basis of existing classes. Inheritance enables classes to support the concept of categorization. In daily life many things are more organized, because they have a very good hierarchical classification. If you do not use hierarchical classification, define all of its properties for each object. With inheritance, each object can define its own special properties.

Encapsulation in C # can use classes to achieve the effect of data encapsulation, so that data and methods can be encapsulated into a single element to facilitate access to data through methods. In addition, you can control how data is accessed. In object-oriented programming, classes are generally used as the basic unit of data encapsulation. class combines the data and the methods of manipulating the data into one unit. When designing a class, you do not want to directly access the data in the class, but you want to access the data through the method. This can achieve the purpose of encapsulating data, convenient for later maintenance, upgrade, but also in the operation of data, more than a layer of judgment, improve security. Encapsulation can also solve the problem of data access permissions, using encapsulation to hide the data, forming a closed space, users can set what data can only be used in this space, which data can be used outside the space. If a class contains sensitive data, some users can access it, and some users cannot access it. If the access to these data is not limited, then the consequences are serious. Therefore, when you write a program, you use different access modifiers for members of the class to define their access levels.

Inheriting inheritance is one of the most important features of OOP. Any class can inherit from another class, that is, the class owns all the members of the class it inherits from. In OOP, the inherited class is called the parent class or the base class. C # provides an inheritance mechanism for classes, but C # supports only single inheritance and does not support multiple inheritance, that is, only one class is allowed to inherit at a time in C #, and multiple classes cannot be inherited at the same time. The inheritance mechanism allows users to augment this class by adding, modifying, or replacing methods in the class to accommodate different application requirements. With inheritance, program developers can construct new classes on the basis of existing classes. Inheritance enables classes to support the concept of categorization. In daily life many things are more organized, because they have a very good hierarchical classification. If you do not use hierarchical classification, define all of its properties for each object. With inheritance, each object can define its own special properties. Each layer of the object simply defines its own nature, and other properties can inherit from the previous layer. In C #, interfaces allow multiple inheritance, which can be achieved by inheriting multiple interfaces, similar to multiple inheritance in C + +. The accessibility of a member is an important issue when inheriting a base class. A subclass cannot access a private member of a base class, but it can access its public members. Both subclasses and external code can access public members. This means that with only these two accessibility, a member can be accessed by the base class and subclass, and also by external code. To solve this problem, C # also provides a 3rd accessibility: protected. Only derived classes can access the protected member, and neither the base class nor the external code can access the protected member. In addition to the protection level of a member, users can define their inheritance behavior for members. A member of a base class can be virtual, and a member can be overridden by a class that inherits it. Subclasses can provide additional execution code for members. This execution code does not delete the original code, and it can still access the original code in the class, but the external code cannot access them. If no other execution is provided, the external code accesses the execution code of the members in the base class. A virtual member cannot be a private member because a member cannot be overridden by subclasses at the same time and cannot access it. A base class can also be defined as an abstract class. Abstract classes cannot be instantiated directly, and it is necessary to inherit the class and instantiate it before using an abstract class. Polymorphic inheritance allows classes derived from the base class to overlap on methods, so objects instantiated from the same base class can be processed using the same syntax. Polymorphism allows instances of subclasses to be directly assigned to variables of the base class, without coercion of type conversions, directly to the method of calling the base class through this variable. Polymorphism is an extremely effective technique for performing tasks on different objects that derive from the same class, using the least code. You can put a set of objects in an array, and then call their methods, which do not have to be the same type of object, in which case the polymorphic effect is reflected. Of course, if they all inherit from a certainClass, you can put these derived classes into an array. If these objects have a method of the same name, you can invoke a method of the same name for each object.

Why C # has inheritance

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.