Depth. NET platform and C # programming note Chapter Sixth inheritance

Source: Internet
Author: User

Sixth Chapter Succession

1. Understanding the concept of inheritance

2. Proficient in using inheritance to establish parent classes and subclasses

3. Understanding the concept of polymorphism

4. The virtual method is overridden to implement polymorphism

1. Inheritance:

In C #, if a class is followed by a colon and followed by another class, then we call the class before the colon

is a subclass, and the class after the colon is the parent class. The relationship that this notation shows is called the inheritance relationship of the class.

Subclass: Derived class

Parent class: base class or Super class

Calling the parent class constructor from base notice point

01. Calling the parent class constructor through base can only be written after the construction of the subclass

02. Calling the parent class through base constructor parameter order is consistent with the parent class's construction parameters

2. Access modifiers

Public: can be accessed anywhere, even across assemblies.

Private: can only be accessed in curly brackets of the current class.

Protected: can only be accessed in the current class, subclasses of the current class, subclasses of subclasses (grandchildren Class).

3. Transitive nature of inheritance

If a Class B inherits from Class A, and Class C inherits Class B, then Class C can also access the non-private members of Class A

Inheritance of the single root (C # does not support multi-inheritance Java and multi-inheritance is not supported)

In C #, a class can have only one parent class.

4. polymorphic

1. Different objects respond differently to the same operation, which is known as polymorphic in object-oriented programming

2. Using polymorphism is for unified invocation

3. Achieve polymorphism in two ways:

Mode one: Using virtual method to realize polymorphism

Steps to achieve polymorphism

01. Define a virtual method with the virtual keyword in the parent class

02. Overriding a virtual method in a parent class with the override keyword in a subclass

The virtual method SayHello () is defined by using the virtual keyword in the normal class person, and then overridden by the override keyword in the subclass student for the parent class's SayHello () method.

Depth. NET platform and C # programming note Chapter Sixth inheritance

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.