C # inheritance Summary-one of the three major mechanisms of Object-Oriented Programming

Source: Internet
Author: User

Inheritance reflects the relationship between classes.

Many things in the world share commonalities. The common part is abstracted as the base class for deriving other classes, which improvesCodeThe reusability makes the code structure clear and easy to read, and it is easy to expand and maintain the code.

C # inheritance can only inherit from one base class, which is different from C ++ inheritance.

The inheritance of C # is passed, that is, B inherits from a and c inherits from B, then C has all the features of.

The inheritance of C # is implicitly public.

If a base class constructor is not displayed in the constructor of the derived class, the compiler automatically inserts a call to the default constructor of the base class before executing the code in the constructor of the derived class, if the base class does not have a default constructor, compilation errors may occur.

For example, the common characteristics of animals include mouth, eyes, and noses. They can move and eat.

So define the base class (including the commonality of animals)

 
Public ClassAnimal {
......
}

 

When defining specific animals, you can inherit from the base class animal. Instead of redefining these basic features, you just need to define your own unique features.

Example: Dog

Public ClassDog: Animal
{
 
// The Compiler automatically inserts a call to the default constructor of the base class before executing the code in the constructor of the derived class.
Public StringBark; // the call of a dog
}
 
Public ClassDog: Animal {
 
// Display the call to a base class constructor In the constructor of the derived class
PublicDog ()
:Base(){
}
Public StringBark;
}

Note:
The value of a derived class can be assigned to the Base class, but the value of a base class cannot be assigned to the derived class, because the base class cannot be converted to a derived class.The base class only contains some features of the derived class.

Animal animal = new dog ();Correct

Dog dog = new animal ();Error

 

 

 

 

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.