C # abstract classes and interfaces

Source: Internet
Author: User

Abstract class

C # agree to declare classes and methods as abstract, that is, abstract classes and abstract methods. Abstract Classes usually represent an abstract concept, which provides a starting point for inheritance. When designing a new object class, it must be used for inheritance. Therefore, in a hierarchy formed by inheritance relationships, leaf nodes should be detailed classes, and tree branches should be abstract classes. Abstract classes have as much code as possible and as little data as possible.


Note: 1. abstract classes cannot be instantiated; 2. the abstract method must be overwritten by the quilt class. 3. if the class includes abstract methods, the class must be defined as an abstract class, whether or not other general methods are included.




Interface

An interface combines implicit public methods and attributes to encapsulate a set of specific functions. Once the class implements the interface, the class can support all attributes and members specified by the interface. The declared interface is syntactically the same as the declared abstract class, but does not agree to provide the running mode of any member in the interface. Therefore, interfaces cannot be instantiated, constructor methods and fields, modifiers such as public and private, and virtual or static declarations. Classes that implement interfaces must implement all methods and attributes of interfaces.

The interface should be declared with the interface, rather than the class. 'I' should be added before the interface name. There must be no modifier before the methods or attributes in the interface, and the method does not have a method body.




Differences between abstract classes and interfaces:


A class is an abstraction of objects, an abstract class is an abstraction of classes, and an interface is an abstraction of behavior.

Assume that the behavior spans objects of different classes and you can use interfaces. For some similar class objects, use the inherited abstract class

From the design point of view, abstract classes discover common things from sub-classes, generalize the parent class, and then inherit the parent class from the sub-classes. The interfaces do not know the existence of the sub-classes, the implementation of the method is not confirmed, and is pre-defined.


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

A derived class can inherit only one base class, that is, it can inherit only one abstract class directly (but it can include multiple abstract classes using one inheritance chain ). On the contrary, classes can use multiple interfaces at will, but this will not produce much difference-the effects of these two cases are similar, except that the method of using interfaces by the consumer 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). They can also be non-Abstract members (with code bodies or virtual ones, in this way, it can be rewritten in a derived class ). On the other hand, interface members must all be implemented on the class using interfaces-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 (only if they are not abstract), protected, internal, or protected internal members (the protected internal members can only ask in the code or derived class of the application ). In addition, the interface cannot contain fields, constructors, destructor, static members, or constants.

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, which are different at the basic level, but can still complete some of the same tasks.

Abstract classes can be implemented by some Members, but interfaces do not include member implementations. Abstract members of abstract classes can be partially implemented by quilt classes. Interface members must implement classes completely, A smart class inherits an abstract class, but multiple interfaces can be implemented.











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.