First recognized inheritance and polymorphism, first recognized inheritance Polymorphism

Source: Internet
Author: User

First recognized inheritance and polymorphism, first recognized inheritance Polymorphism

What is inheritance?

Inheritance uses existing class definitions as the basis to create new class technologies. New class definitions can add new data or functions, or use existing class functions.

Inheritance is a very important feature in object-oriented programming. In the two classes with inheritance relationships, the subclass not only has its own independent members, but also has members of the parent class.

The inheritance relationship represents an arrow in the class diagram, and the arrow points to the parent class.

Base keyword and protected Modifier

All know that this indicates the current instance. It can be used to access the members of the parent class. in C #, there is a keyword base, which indicates the parent class and can be used to access the members of the parent class.

In the subclass, you can use base to call the attributes of the parent class. In fact, you can also use the base keyword to call the methods of the parent class and the constructor of the parent class.

If a member in the parent class is modified using private, the member will act as a private member and cannot be accessed by any other class. If it is set to a public member, any class can access the member.

In C #, another access modifier protected is provided. The access modifier member is allowed to be accessed by its subclass, but not by other non-subclass access.

Differences between public, private, and protected

Modifier class, internal subclass, other classes

Public: Yes.

Can protected be?

Public or not.

It can be seen that the three access modifiers restrict access to class members: private> protected> public

Subclass Constructor

Call the parent class constructor implicitly.

Is a application

Inherited Value

Appreciate the inheritance features

Inheriting the relationship that simulates the real world, oop emphasizes that everything is an object, which conforms to our thinking direction of object-oriented programming.

Inheritance achieves code reuse and reasonable use of inheritance, making the code more concise

Inheritance makes the program structure clear, and the Child class and parent class have a clear hierarchy. The ultimate goal is to make the child class focus only on the behavior and Status of the Child class, and do not need to focus on the behavior and status of the parent class.

Solve the Problems Caused by inheritance

Public virtual string SayHi (){
// Method body
}

Such methods that use virtual keywords are called virtual methods. The virtual method has a method body.

Syntax

Access modifier virtual return class method name (){
// Method body

}

 

Public override string SayHi (){
// Method body
}

A method such as the override keyword is called method rewriting. Virtual methods can be overwritten.

Syntax

Access modifier override

Return class method name (){
// Method body
}

What is polymorphism

Implementation of Polymorphism

Override of implementation methods

Define the SayHi () method in the parent class and use the virtual keyword to define it as a virtual method.

Define the sub-class's own SayHi () in the sub-class and use the override keyword to override the SayHi () method of the parent class.

Defines parent variables and initializes parent variables with subclass objects.

Polymorphism literally means multiple forms. It means that different interpretations can be made for different objects to produce different execution results.

Polymorphism can be achieved using virtual methods.

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.