somesogoood

Monday, February 16, 2009

interface and abstract class distinction

Surface questions: interface and abstract class distinction 
One difference between the two not the same as the concept of expression. Abstract class is a class of things a high degree of polymerization, then the succession of abstract class for the subclass, the abstract class for example, are "yes" relationship; and the interface is the definition of a code of conduct, so for the realization of the sub-class interface for , compared with the interface is "act in accordance with the interfaces required to complete." 

Distinction between two types of abstract class in the definition of methods, they can give way to the realization of some, it may not give; As for the interface is defined by the method which can give some realize. 

Distinction between three categories of succession for the two involved in the realization of Ways are different. Class inheritance is defined for abstract class abstract methods, can be rewritten, that is to say, you can use abstract class extension of the method; interface for the definition of class methods or properties, in the inherited class must give the corresponding realize the methods and properties. 

The difference between the four, in the abstract category, add a method, then inherit class can not make any deal with; As for interface, it necessary to amend the succession of categories, providing a new definition of the method. 

Give the following easy comparison between the two forms. 

  
Interface 
Abstract class 
Multiple inheritance 
Support 
Do not support 
Type restrictions 
No 
Has only reference types are 
Ways to achieve 
Types of succession must give way to realize 
Class inheritance can not give 
Scalability 
Is too much trouble 
Relatively flexible 
Multi-inheritance 
Is too much trouble to use virtual function required 
More flexible




==
c # interface and abstract class distinction 
  U.S. will easily confuse these two, I also like listening to the teacher's design patterns Chien-Chung Lee, he is also old the abstract class as interface, I get even more confused, so to find some online information. 
       First, abstract categories: 
       Abstract class is a special category, but can not be instantiated; In addition, with other properties; important is abstract class may include abstract methods, this is not the ordinary type. Abstract methods can only be a statement in the abstract class and does not contain any realize, derived class must cover them. In addition, the abstract class can be derived from an abstract class, you can override the base class abstract method can also be not covered, if not cover, then its derived class must cover them. 

        Second, interface: 
       Interfaces are reference types, similar to categories, and the similarities between abstract class has three points: 
        One can not be instantiated; 
        2, including the method statement did not realize; 
        3, derived classes must realize unrealized methods are abstract methods of abstract class, interface is all the members (not only methods, including other members); 

        In addition, the interface has the following characteristics: 
Besides interface can contain methods, but also can contain attributes, index, and events, and these members are defined as public domain. In addition, should not contain any other members, such as: constant domain, constructor, destructor, static members. A class can directly inherit multiple interfaces, but only directly inherit a class (including the abstract class). 


       Three abstract classes and interfaces of the distinction: 
       1. Class is an abstract object can be understood as the abstract class type as an object into an abstract class is called abstract class. The interface is just a behavior or require Microsoft custom interface is always able to bring the word after paragraph, to demonstrate that their statements are a class of type "I can do more 。。。". abstract class are defined in a series of closely related categories, and the interface is the relationship between the majority of osteoporosis but to achieve a particular function category . 
       2. Interface basically does not have any specific features of the succession, it is merely a commitment to be able to call the method; 
       3. A class can realize a number of interfaces, but only the expansion of a parent class 
       4. Interface can be used to support the callback, but the succession did not have this feature. 
       5. Abstract class can not be sealed. 
       6. Abstract class to achieve the specific method defaults to false, but realize interface class interface methods are by default non-virtual, of course, you can also virtual declaration. 
       7. (Interface) with a similar non-abstract class, abstract class must also be in kind of the base class list of interfaces available to all members of its own to achieve. However, the permit will interface methods of abstract class is mapped to abstract methods. 
       8. Oop abstract class implements a principle of variable separation and can not be changed. Abstract class and interface is defined as immutable, and the variable sub-class seat to be realized. 
       9. Good interface should be defined is a specific functionality, rather than multi-function, otherwise the pollution caused by interface. If a category is only realize the interface of a function of the interface had to go to achieve the other methods, called interface pollution. 
      10. As far as possible avoid the use of inheritance to achieve the formation of functional, but the use of black-box reuse, that is the target portfolio. Because an increase in the level of inheritance, resulting in the most direct consequence is that when you call this group in a certain category, it is necessary to them all loaded into the stack! Consequences can be imagined. (Combination of understanding the principle of stack). At the same time, concerned friends have noticed that Microsoft can build a category, they often use a combination of methods of the object. For example, asp.net Medium, Page Class, Server Request has attributes such as, but in fact they are the object of a certain category. Page class used this object to call the other type of methods and properties, this is a very basic design principles. 
      11. If the abstract class realize interface, the interface methods can be mapped to the abstract class as abstract methods without having to realize, and in the abstract class to achieve the sub-class interface methods. 
    
       Fourth, abstract class and interface use: 
       1. If the component is expected to create multiple versions, then the creation of abstract class. Abstract class to provide easy ways to control the component version. 
       2. If the function will be to create a wide range of wide differences between the use of objects, use the interface. If you want to design small and concise functional blocks, use the interface. 
       3. If you want to design a large functional unit, then use the abstract class. If you want to realize in all the components which provide common functions have been achieved, then the use of abstract class. 
       4. Abstract class used to close the object; and interface suitable for non-related category provide a common function. 


  The following is what I have seen a few online image analogy, really good, huh, huh: 
1. The aircraft fly, birds fly, they inherit the same interface "Fly"; but F22 aircraft belonging to an abstract class, abstract class pigeons are birds. 
2. Just like the wooden doors are iron gate door (abstract class), you want a door I can not (can not be instantiated), but I can give you a concrete steel doors or wooden doors (many states); and only are able to Door, you should not say it is a window (single inheritance); a door can be locked (interface) can also have doorbell (many realize). Door (abstract class) defines what you are, interface (lock) provides what you can do (an interface can only be the best thing to do, you can not ask the lock can also sound bar (interface pollution)).

No comments: