(original) C # learning note 09--defining class 06--interfaces and abstract classes

Source: Internet
Author: User

9.6 Interfaces and abstract classes

Both abstract classes and interfaces contain members that can be inherited by derived classes. Interfaces and abstract classes cannot be instantiated directly, but variables of these types can be declared. If you do this, you can use polymorphism to assign variables that inherit both types of objects to them. These variables are then used to use these types of members, but they cannot be accessed directly by the derived object's
His members.

Here's a look at their differences. Derived classes can inherit only one base class, that is, only one abstract class can be inherited directly (but with one inheritance chain containing multiple abstract classes). Instead, classes can use any number of interfaces. But that doesn't make much of a difference-the two cases have similar effects. There is a slight difference in the way the interface is used.

Abstract classes can have abstract members (without code bodies and must be implemented in derived classes, otherwise the derived classes themselves must also be abstract) with non-abstract members (they have the code body, or they can be virtual, so they can be overridden in a derived class). On the other hand, interface members must all be implemented on classes that use interfaces--they have no code body. Also, by definition, interface members are public (because they tend to be used externally), but members of an abstract class can be private (as long as they are not abstract), protected, internal, or protected internal members (where protected internal members can only be accessed in the application's code or derived classes). In addition, an interface cannot contain a field, constructor, destructor, static member, or constant .

  Note: Abstract classes are primarily used as base classes for object families, sharing some of the main features , such as common purposes and structures. interfaces are primarily used for classes, which differ at the base level, but can still accomplish some of the same tasks .

For example, suppose an object family represents a train, and the base class Train contains the core definitions of the train, such as the specifications of the wheels and the type of engine (which can be steam engines, diesel engines, etc.). 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 do this, derive some classes, such as: Passengertrain, Freighttrain, and 424DoubleBogey, as shown in 9-14.

  

The Car object series can also be defined in the same way, using car abstract base classes, which derive from the compact, SUV, and Pickup,car and train can even be derived from the same base class as shown in Vehic le,9-15.

Some classes in the hierarchy now share the same attributes because they are intended to be the same, not because they derive from the same base class . For example, Passengertrain, Compact, SUV, and pickup are all capable of transporting passengers (the same task), so they all have ipassengercarrier interfaces , Freighttrain and pickup can ship goods (the same task ), so they all have iheavyloadcarrier interfaces , as shown in 9-16.

Before a more detailed breakdown, the object system is decomposed in this way, so that you can clearly see which situation is appropriate for the use of an abstract class and which is appropriate for the interface. The results of this example are not available if you use only interfaces or only abstract inheritance.

(original) C # learning note 09--defining class 06--interfaces and abstract classes

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.