The difference between abstract classes and interfaces in C#

黄舟
Release: 2017-02-27 11:33:21
Original
1155 people have browsed it

It is easy for everyone to confuse the two, and I am the same. When listening to teacher Li Jianzhong’s design patterns, he always refers to abstract classes as interfaces, which makes me even more confused, so I looked for some information on the Internet. Information.
1. Abstract class:
Abstract class is a special class, but it cannot be instantiated; in addition, it has other characteristics of the class; the important thing is that the abstract class can include abstract methods, which are ordinary classes What cannot be done. Abstract methods can only be declared in abstract classes and do not contain any implementation. Derived classes must override them. In addition, an abstract class can be derived from an abstract class and can override the abstract methods of the base class or not. If not, its derived class must override them.

2. Interface:
Interface is a reference type, similar to a class, and has three similarities with abstract classes:
1. It cannot be instantiated;
2. Contains unimplemented Method declaration;
3. Derived classes must implement unimplemented methods. Abstract classes are abstract methods, and interfaces are all members (not only methods but other members);

In addition, interfaces have the following characteristics :
In addition to methods, interfaces can also contain properties, indexers, and events, and these members are all defined as public. In addition, it cannot contain any other members, such as constants, fields, constructors, destructors, and static members. A class can directly inherit multiple interfaces, but can only directly inherit one class (including abstract classes).

3. The difference between abstract classes and interfaces:
1. A class is an abstraction of an object. An abstract class can be understood as treating a class as an object. The abstracted class is called an abstract class. The interface is just A specification or regulation of behavior. Microsoft's custom interface is always followed by an able field, proving that it expresses a class "I can do it...". Abstract classes are more defined between a series of closely related classes. , and interfaces are mostly classes that have loose relationships but all implement a certain function.
2. The interface basically does not have any specific characteristics of inheritance, it only promises methods that can be called;
3. A class Several interfaces can be implemented at a time, but only one parent class can be extended
4. Interfaces can be used to support callbacks, but inheritance does not have this feature.
5. Abstract classes cannot be sealed.
6. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class that implements the interface are non-virtual by default. Of course, you can also declare them as virtual.
7. (Interface) and Similar to non-abstract classes, an abstract class must also provide its own implementations for all members of the interfaces listed in the class's base class list. However, abstract classes are allowed to map interface methods to abstract methods.
8. Abstract classes implement a principle in oop, separating the mutable and the immutable. Abstract classes and interfaces are defined as immutable, and mutable seat subclasses are implemented.
9. A good interface definition should have specific functionality rather than multi-function, otherwise it will cause interface pollution. If a class only implements one function of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Try to avoid using inheritance to implement component functions, but use black box reuse, that is, object combination. Because the levels of inheritance increase, the most direct consequence is that when you call a certain class in this class group, you must load them all into the stack! The consequences can be imagined. (Understood in conjunction with the stack principle). At the same time, interested friends can notice that when Microsoft builds a class, it often uses the object combination method. For example, in asp.net, the Page class has attributes such as Server Request, but in fact they are all objects of a certain class. Using this object of the Page class to call methods and properties of other classes is a very basic design principle.
11. If an abstract class implements an interface, you can map the methods in the interface to the abstract class as abstract methods without having to implement them, and implement the methods in the interface in the subclass of the abstract class.

4. Use of abstract classes and interfaces:
1. If you anticipate creating multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions.
2. If the created functionality will be used across a wide range of disparate objects, use interfaces. If you want to design small and concise functional blocks, use interfaces.
3. If you want to design a large functional unit, use abstract classes. If you want to provide common implemented functions among all implementations of the component, use abstract classes.
4. Abstract classes are mainly used for closely related objects; interfaces are suitable for providing common functions for unrelated classes.

The following are a few metaphors I saw on the Internet, they are really good, hehe:
1. Planes can fly and birds can fly. They both inherit the same interface "fly"; but F22 belongs to the aircraft abstract class and pigeons belong to the bird abstract class.
2. Just like iron doors and wooden doors are all doors (abstract class), I can’t give you the door you want (cannot be instantiated), but I can give you a specific iron door or wooden door (polymorphism); and It can only be a door, you cannot say it is a window (single inheritance); a door can have a lock (interface) or a doorbell (multiple implementations). The door (abstract class) defines what you are, and the interface (lock) stipulates what you can do (an interface should only do one thing, you can't ask the lock to make a sound (interface pollution)).

The above is the difference between abstract classes and interfaces in C#. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!