C # Study Notes (3): abstract classes and interfaces

Source: Internet
Author: User

Similarities between abstract classes and interfaces:

Abstract classes and interfaces both contain members that can be inherited by Derived classes. Both interfaces and abstract classes cannot be directly instantiated, but their variables can be declared. In this case, you can use polymorphism to specify the variables that inherit the two types of objects to them. Then use these variables to use these types of members, but cannot directly access other members of the derived object.

 

Differences between abstract classes and interfaces:

A derived class can only inherit one base class, that is, only one abstract class can be inherited directly (but multiple abstract classes can be contained in one inheritance chain ). On the contrary, classes can use any number of interfaces.

Abstract classes can have abstract members and non-Abstract members.

Abstract Member: it has no code body and must be executed in the derived class. Otherwise, the derived class itself must be abstract.

Non-Abstract members: They have code bodies or virtual ones, so that they can be rewritten in a derived class.

All interface members must be executed on the class using the interface-they have no code body.

Interface members are defined as public, abstract class members can also be private protected, internal, or protected internal members (the protected internal members can only be accessed in the application code or derived classes ). In addition, the interface cannot contain fields, constructors, destructor, static members, or constants.

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.