I. abstract class:
Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.
Ii. interface:
The interface is of reference type. It is similar to a class and has three similarities with the abstract class:
1. It cannot be instantiated;
2. contains an unimplemented method statement;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );
In addition, interfaces have the following features:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).
Iii. Differences between abstract classes and interfaces:
1. class is the abstraction of objects. abstract classes can be understood as classes as objects. abstract classes are called abstract classes. the interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field behind it to prove that it represents a class "I can do it... ". Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended.
4. interfaces can be used to support callback, but inheritance does not.
5. the abstract class cannot be sealed.
6. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them as virtual.
7. Similar to a non-abstract class, an abstract class must provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods.
8. abstract classes implement a principle in OOP that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented.
9. A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined (based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in Asp.net, the page class has server request and other attributes, but in fact they are all objects of a certain class. This object of the page class is used to call the methods and attributes of other classes. This 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 an abstract method without having to implement it. Instead, you can implement the methods in the subclass of the abstract class.
Iv. Use of abstract classes and interfaces:
1. If you want to create multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions.
2. If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces.
3. If you want to design a large functional unit, use the abstract class. If you want to provide general implemented functions among all the implementations of the component, use the abstract class.
4. abstract classes are mainly used for closely related objects. interfaces are suitable for providing general functions for irrelevant classes.
The following are some of the Image metaphors I have seen on the Internet. They are really good:
1. Planes fly and birds fly. They all inherit the same interface "fly". However, f22 belongs to the aircraft abstract class and pigeon belongs to the bird abstract class.
2. just like all doors (abstract class), I can't give you a door (I can't instantiate it), but I can give you a specific door or wooden door (polymorphism ); 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 ). A door (abstract class) defines what you are and an interface (LOCK) specifies what you can do (one interface is best to do only one thing, you cannot require the lock to make sound (interface pollution ).
Http://kb.cnblogs.com/page/41836/
NET provides an interface, which is different from the class or struct type definition. In some cases, the interface seems to be the same as the abstract class, so some people think that the. NET interface can be used to replace the abstract class completely. In fact, interfaces and abstract classes have their own strengths and weaknesses. Therefore, they are often used together in applications to complement each other.
Next, let's talk about the differences between abstract classes and interfaces.
Difference 1: the two concepts are different. Abstract classes are highly aggregated for a type of things. Therefore, for subclasses that inherit abstract classes, they belong to the "yes" relationship. Interfaces define behavior specifications, therefore, for the subclass that implements the interface, compared with the interface, it is "the behavior needs to be completed according to the interface ". These sounds a bit false. For example. For example, a dog is a general term for all dogs and animals. If a dog is a dog and a dog is a dog, the general characteristics of the dog will be found in jingha and a sheepdog, compared with jingha and sheepdog, a dog belongs to the abstract type of such things. For the action of "calling", a dog or a bird can also be called. Obviously, the former is equivalent to an abstract class, while the latter is an interface.
Difference 2: when defining a type method in an abstract class, the implementation part of the method can be provided or not. For an interface, the implementation part of the method defined in the abstract class cannot be provided.
For example:
Public abstract class abstest
{
Public Virtual void test ()
{
Debug. writeline ("test ");
}
Public abstract void newtest ();
}
Public interface itest
{
Void test ();
Void newtest ();
}
Difference 3: The Inheritance class has different implementations for the methods involved in the two. For abstract methods defined by abstract classes, the inherited classes do not need to be overwritten. That is to say, they can be extended. For methods or attributes defined by interface classes, corresponding methods and attribute implementations must be provided in the inheritance class.
Difference 4: In an abstract class, if you add a method, the inheritance class does not need to be processed. For an interface, you need to modify the inheritance class and provide a new defined method.
Knowing the differences between the two, let's talk about the advantages of interfaces over abstract classes.
Benefit 1: An interface can be applied not only to the reference type, but also to the value type. Abstract classes can only act on the reference type.
Benefit 2:. Net type inheritance can only be a single inheritance, that is, a type can only inherit one type, but can inherit multiple interfaces. In fact, I agree with this point. Multi-inheritance will make the inheritance tree messy.
Benefit 3: Because the interface only defines attributes and methods, but has little to do with the actually implemented types, the interface can be reused by multiple types. Abstract classes are more closely related to inherited classes.
Benefit 4: through interfaces, you can reduce the type exposure attributes and methods, so as to facilitate the protection of type objects. When an interface type may contain other methods or attributes, but a method returns an interface object, the caller can only use the methods or Attributes provided by the interface, access the related elements of the object, which can effectively protect other elements of the object.
Benefit 5: Reduce the value-type unpacking operation. For the value-type data defined by struct, whenever a collection is stored, it is necessary to unpack the data. In this case, the combination of struct and interface is used to reduce the unpacking operation.
See the methods provided in the following article.
Http://blog.csdn.net/Knight94/archive/2006/10/08/1326326.aspx
Compared with abstract classes, interfaces have many advantages, but interfaces have a fatal weakness, that is, the method and attribute defined by the interface can only be relative to the type inherited from it (unless the Function Identifier defined by the excuse is modified in the inheritance class). For multi-layer inheritance relationships, it is difficult to implement an interface. If each type is implemented by inheriting the interface, it is cumbersome to write the code first, and sometimes the execution result is incorrect, especially when the child type objects are implicitly converted to base class objects for access.
At this time, you need to use interfaces and virtual methods. See the implementation method of idisposable in the inheritance type.
Http://blog.csdn.net/Knight94/archive/2006/10/10/1329214.aspx
In fact, in inheritance, the interface or abstract class is used. The interface is fixed and conventional. Therefore, the corresponding methods and attributes of the interface must be provided in the inheritance class. For an abstract class, the implementation of the definition method of the abstract class runs through the entire inheritance tree. Therefore, the implementation or rewriting of the method is uncertain. Therefore, abstract analogy interfaces are more flexible.
The following is a simple comparison table between the two.
Interface abstract class
Multi-inheritance is not supported.
No type restriction. It can only be a reference type.
The method implementation inheritance class must be provided.
Scalability is relatively troublesome and relatively flexible
Multi-layer inheritance is troublesome and requires flexible use of virtual functions.
In general, interfaces and abstract classes are language methods provided by. Net to better implement the inheritance relationship between types, and they complement each other. Therefore, the key to the problem is that it is vital to apply the two methods to the program.
This article Reprinted from the network base camp: http://www.xrss.cn/Dev/DotNet/200792016649.Html