C # Getting Started classic-9.6 interfaces and abstract classes

Source: Internet
Author: User

Abstract classes and interfaces contain members inherited from 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.

A derived class can inherit only one base class, that is, only one abstract class can be inherited (but multiple abstract classes can be contained in one inheritance chain ). On the contrary, classes can use any number of interfaces. But there will be no big difference-the two situations have a similar effect. The method of using interfaces is slightly different.

Abstract classes can have abstract members (without code bodies and must be implemented in the derived classes; otherwise, the derived classes must also be abstract) and non-abstract classes (they have code bodies or virtual ones, in this way, it can be rewritten in the derived class ). On the other hand, interface members must all be implemented on the class using the interface-because they do not have code bodies. In addition, according to the definition, the interface members are public (because they tend to be used externally), but the abstract class members can be private (as long as they are not abstract), protected, internal, or protected internal members (the protected internal members can only be accessed in the application code or derived class ). In addition, the interface cannot contain fields, constructors, destructor, static members, or constants.

Note:

These two types are used for completely different purposes. Abstract classes are mainly used as the base classes of object series and share some main features, such as the common purpose and structure. Interfaces are mainly used for classes. These classes are different at the basic level, but some identical tasks can still be completed.

For example, if an object series represents a train, the base train contains the core definition of the train, such as the wheel specifications and engine types (such as steam engines and diesel engines ). But this class is abstract, because there is no "normal" train. To create an actual train, you need to add features to the train. To this end, some classes are derived, such as passengertrain, freighttrain, and doublebogye, as shown in:

The car object series can also be defined in the same way. The abstract class of the car class is used. Its Derived classes include compact, SUV, and pickup. Car and train can even be derived from the same basic class vehicle, as shown in:

At present, some classes in the hierarchies share the same features because they have the same purpose, rather than because they are derived from the same base class. For example, passengertrain, compact, SUV, and pickup can both transport passengers, so they all have ipassengercarrier interfaces, freighttrain and pickup can transport goods, so they both have iheavyloadcarrier interfaces, as shown in:

Before detailed subdivision, the object system is decomposed in this way, you can clearly see which situation is suitable for the use of abstract classes and which situation is more suitable for the use of interfaces.

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.