C # abstract classes and interfaces

Source: Internet
Author: User

Abstract class

C # the class and method can be declared 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 specific 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 a class contains abstract methods, the class must be defined as an abstract class, whether or not it contains other general methods.




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 Declaration interface is syntactically identical to the declaration abstract class, but it cannot provide the execution method of any member of 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. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kphn0cm9uzz4gicagvdo/do + 7PGx7DSqrzToa5J ". The methods or attributes in the interface cannot contain modifiers or methods without method bodies.




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.

If the behavior spans objects of different classes, 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 contain 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. If this is done, 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 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, but this does not produce much difference-the effects of these two cases are similar, but 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) with non-Abstract members (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 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 (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.

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.

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











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.