Same point:
- 1. All can be inherited
- 2, can not be instantiated
- 3. You can include a method declaration
- 4. Derived classes must implement methods that are not implemented
Difference:
- 1. Abstract base classes can define fields, properties, and method implementations. An interface can only define properties, indexers, events, and method declarations, and cannot contain fields.
- 2, abstract class is an incomplete class, need further refinement, and interface is a code of conduct. Microsoft's custom interface is always behind the able field, proving that it is stating a class of "I can do ... ”
- 3, interfaces can be multiple implementations, abstract classes can only be inherited by a single
- 4, the abstract class is more defined in a series of closely related classes, and the interface is mostly loose, but all implement a function of the class
- 5, abstract class is a series of related objects abstracted from the concept, so reflects the internal commonality of things; an interface is a functional contract defined to satisfy an external invocation, and therefore reflects the external nature of the thing
- 6, the interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method
- 7, the interface can be used to support callbacks, and inheritance does not have this feature
- 8, the specific method of the implementation of the abstract class defaults to virtual, but the implementation of the interface of the class interface method is not virtual, of course, you can also declare as virtual
- 9. If an abstract class implements an interface, you can map the method in the interface to an abstract class as an abstract method without having to implement the method in the subclass of the abstract class
Usage Rules:
- 1. Abstract classes are primarily used for closely related objects, and interfaces are best suited to provide common functionality for unrelated classes
- 2, if you want to design a large functional unit, then use the abstract class, if you want to design a small and concise function block, then use the interface.
- 3. If you expect to create multiple versions of a component, create an abstract class. The interface cannot be changed once it is created. If a new version of the interface is required, a completely new interface must be created.
- 4. Use an interface if you create a feature that will be used across a wide range of heterogeneous objects, or an abstract class if you want to provide common, implemented functionality across all implementations of a component.
- 5, the Analysis object, refines the internal generality to form the abstract class, uses to represent the object essence, namely "what". The interface is preferred when the external invocation or function needs to be expanded
- 6, good interface definition should be a functional, rather than multifunctional, or cause interface pollution. If a class just implements one of the functions of this interface, and has to implement other methods in the interface, it is called interface pollution
- 7, try to avoid the use of inheritance to achieve the building function, but use black box reuse, that is, the object combination. Because of the increasing level of inheritance, the most immediate consequence is that when you call one of these classes, you have to load them all into the stack! The consequences are conceivable. (in conjunction with the stack principle). At the same time, the heart of friends can notice that Microsoft in the construction of a class, many times the use of the object combination method. For example, in ASP. NET, the page class has properties such as server request, but in fact they are all objects of a class. Using this object of the page class to invoke methods and properties of another class is a very basic design principle
For example:
Window forms can be designed with abstract classes, can put public actions and attributes into an abstract class, let form and dialog box inherit from this abstract class, then according to their own needs to expand and perfect.
The print operation can be provided as an interface to each form that requires this functionality, because the content of the form is different, and it is necessary to implement its own printing function according to their own requirements. It is only called through the interface when printing, regardless of the form to be printed.
Commonality, individuality and choice:
I don't agree with the book that C # recommends using interfaces (Interface) instead of abstract classes, and emphasizes the many benefits of using interfaces, which I disagree with, from the list above, there are still a lot of differences between the two, The existence of this difference inevitably determines the different scenarios, for example, in the abstract base class can provide a default implementation of some methods, so as to avoid repeating in subclasses to implement them, improve the reusability of code, which is the advantage of abstract classes, and the interface can only contain abstract methods. As to when to use the abstract base class when using the interface is critical or how the user views the relationship between the inherited classes, the user is more concerned about the personality differences between them or their common connection. Give an example of life to illustrate.
If you are given three objects that are human, fish, and frogs, so that you can design a base class for them to summarize the connection between them, then the first thing you must feel is that their individual differences are large, it is difficult to abstract the common denominator, but if you generalize the commonality of their behavior, you may think that you will realize that they can swim. It's just a different way of swimming. Then you should consider using interfaces instead of abstract base classes for three reasons:
InterfaceIswim {voidSwim ();} Public classPerson:iswim { Public voidSwim () {//swimming in person ' s style. } } Public classFrog:iswim { Public voidSwim () {//swimming in frog ' s style. } } Public classFish:iswim { Public voidSwim () {//swimming in fish ' s style. } }
- 1. Personality is more than common.
- 2, the difference between the personality has some of the same behavior.
- 3, the same behavior of the implementation of a larger difference.
At this time give you three objects, respectively, carp, carp, goldfish, still let you design the base class to summarize the relationship between them, then you first realize that they are all belong to fish, followed by their way of swimming may be slightly different, then should use abstract base class instead of interface, contrast the above example, There are also three reasons:
Abstract Public classFish {Abstract Public voidSwim ();} Public classCrucian Carp: Fish { Public Override voidSwim () {//Swim like a crucian carp } } Public classCarp: Fish { Public Override voidSwim () {//Swim like a carp } } Public classGoldfish: Fish { Public Override voidSwim () {//Swim like a goldfish } }
- 1. Generality is more than individuality
- 2. There must be the same attributes and behaviors between individuals with the same generality.
- 3, the same behavior of the implementation of a certain difference
The third reason for observing the use of interfaces or the use of abstract base classes is the same, which describes the concept of multi-state in an object-oriented way, which is implemented by overriding the parent class, and invoking the corresponding method at run time based on the object reference being passed. The second reason begins to diverge, and the interface emphasizes the same behavior among the inherited objects, while the abstract class also emphasizes the same attributes among the inherited objects. The real reason for separating the interface from the abstract base class is that it is summed up as follows:
- Interfaces are used when searching for functional commonalities between objects with large differences.
- Use abstract base classes when looking for functional differences among more common objects.
Through the same and different comparisons, we can only say that the interface and abstract classes, strengths, but no advantage. In the actual programming practice, we have to consider the specific circumstances to the discretion, but the following experience and accumulation, may give us some inspiration, in addition to some of my accumulation, many come from the classics, I believe that can withstand the test. So in the rules and occasions, we learn these classics, the most important thing is to apply, of course, I will opinion broad home smile, crossing please continue.
Rules and occasions:
- 1, keep in mind that one of the most important principles of object-oriented thinking is interface-oriented programming.
- 2, with the help of interfaces and abstract classes, many of the ideas in the 23 design patterns have been cleverly implemented, and I think the essence is simply: oriented toward abstract programming.
- 3, abstract classes should be used primarily for closely related objects, and interfaces are best suited to provide common functionality for unrelated classes.
- 4, the interface focuses on the can-do relationship type, while the abstract class is biased to the is-a-type relationship,
- 5, the interface multi-definition object behavior, abstract class multi-definition object properties,
- 6, the interface definition can use public, Protec Ted, internal, and private modifiers, but almost all of the interfaces are defined as public, so there's no need to say more.
- 7, "interface unchanged" is an important factor to consider. Therefore, when an extension is added by an interface, the new interface should be added, and the existing interface cannot be changed.
- 8, as far as possible to design the interface as a function of a single function block, for example, the. NET framework, IDisposable, IDisposable, IComparable, IEquatable, IEnumerable includes only one public method.
- 9, the uppercase "I" in front of the interface name is a convention, just as the field names begin with an underscore, adhere to these principles.
- 10, in the interface, all methods default to public.
- 11, you can create an abstract class if you anticipate that a version problem will occur. For example, to create a dog, a chicken (Chicken), and a duck (Duck), you should consider abstracting the animal (Animal) to deal with the possible future of the wind horse cattle. Adding a new member to an interface would force the requirement to modify all derived classes and recompile, so the problem with versioning is best implemented as an abstract class.
- 12, a non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstract accessor.
- 13, cannot use the New keyword for an abstract class, nor can it be sealed, because an abstract class cannot be instantiated.
- 14, you cannot use the static or virtual modifier in an abstract method declaration.
This article from the CSDN blog, reproduced please indicate the source:
Http://blog.csdn.net/fxh_hua/archive/2009/08/20/4464739.aspx
The same points and differences between abstract and interface (interface) in C #