C # Inheritance

Source: Internet
Author: User

Inheritance (C # Programming Guide)

Classes can inherit from other classes. This is done by placing a colon after the class name when declaring the class, and then specifying the class (that is, the base class) to inherit from after the colon. For example:

public class A
{
  public A() { }
}
public class B : A
{
  public B() { }
}

The new class (that is, the derived class) gets all the non-public data and behavior of the base class and all other data or behavior that the new class defines for itself. Therefore, the new class has two valid types: The type of the new class and the type of the class it inherits.

In the example above, Class B is both a valid B and a valid A. When you access a B object, you can use a cast operation to convert it to a object. Casting does not change the B object, but your view of B objects will be limited to the data and behavior of a. After casting B to a, you can cast it back to B. Not all instances of a are cast to B, and only those instances that are actually instances of B can be cast to B. If Class B is accessed as type B, the data and behavior of both class A and Class B can be obtained at the same time. The ability of an object to represent multiple types is called polymorphism. For more information, refer to this article polymorphism.

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.