C # - the difference between interfaces and abstract classes?

Reproduced in  https://www.cnblogs.com/jian1125/p/10530125.html

 

First, the interface

The interface is a reference type refers to the definition of agreements, other types of interfaces to achieve, to ensure that they support certain operations. Members must specify the interface provided by the class or implement its other interfaces. Similar to the class, the interface may include methods, properties, events, and indexers as members.

1, meaning the presence of the interface is to achieve polymorphism;


2, the interface can contain methods (properties, events, index);


3, members of an interface can not have any implementation;


4, the interface can not be instantiated; static class, an abstract class can not be instantiated


5, members of an interface can not have access modifiers (the default is public);


6, sub-class that implements the interface must implement all the member functions of interfaces;


7, when implemented method interface subclass, without any keywords, can be directly implemented. (Abstract class, need to add virtual methods override)

Second, the abstract class

Abstract classes, in object-oriented process, all classes of objects are depicted, in turn, insufficient data to which class object portrayed particular, such a class is an abstract class

1, abstract method can only occur in an abstract class, an abstract class but may comprise an ordinary method.

2, the abstract methods defined in the parent class can not be realized.

3, an abstract class can not be instantiated (i.e., it is not new).

4, abstract classes and abstract methods need to add the abstract keyword.

5, sub-class implements the abstract methods of the parent class, you need to add the override keyword.

6, if all the abstract methods abstract class subclass is not abstract class, subclass must override the parent class abstract class.

 

Third, the difference between abstract classes and interfaces

 

Same point

  1 can not be instantiated;

  2. The method declaration contains unrealized

  3. The derived class must implement method is not implemented, the abstract class is abstract methods, interfaces are all members (not just the method includes the other members)

 

difference

 

  1. The interface can be multiple inheritance, abstract classes can not implement multiple inheritance.

  2. The interface can only define abstract rules, an abstract class can not only define abstract rules also provide members have achieved

  3. Interface Specification is a set of behaviors, abstract class is a class incomplete, it focuses on the concept of group

  4. The interface supports callback, callback abstract class can not be achieved, because inheritance is not supported.

  The interface contains only the methods, properties, indexers, event signature, but can not define fields and methods comprising achieve abstract class defines properties, fields, implemented method comprising

  6. The interface may be applied to the value and reference types, the abstract class only act on the reference type (e.g.: Struct interfaces can inherit)

  7. The abstract class should be used primarily close objects, and the interface is adapted to provide common functionality unrelated to the class

  8. Can-Do focus on the interface type of relationship, the abstract class type emphasis on IS-A relationship.

  9. Multi-Interface defines behavior of the object, multiple abstract class defines the properties of the object.

  10. If you want to design small, concise function block, the interface is used, if the functional unit to be designed large, the use of the abstract class

  11. The interface corresponds to an important principle of object-oriented programming ideas: Oriented Programming Interface

 

First, the interface

The interface is a reference type refers to the definition of agreements, other types of interfaces to achieve, to ensure that they support certain operations. Members must specify the interface provided by the class or implement its other interfaces. Similar to the class, the interface may include methods, properties, events, and indexers as members.

1, meaning the presence of the interface is to achieve polymorphism;


2, the interface can contain methods (properties, events, index);


3, members of an interface can not have any implementation;


4, the interface can not be instantiated; static class, an abstract class can not be instantiated


5, members of an interface can not have access modifiers (the default is public);


6, sub-class that implements the interface must implement all the member functions of interfaces;


7, when implemented method interface subclass, without any keywords, can be directly implemented. (Abstract class, need to add virtual methods override)

Second, the abstract class

Abstract classes, in object-oriented process, all classes of objects are depicted, in turn, insufficient data to which class object portrayed particular, such a class is an abstract class

1, abstract method can only occur in an abstract class, an abstract class but may comprise an ordinary method.

2, the abstract methods defined in the parent class can not be realized.

3, an abstract class can not be instantiated (i.e., it is not new).

4, abstract classes and abstract methods need to add the abstract keyword.

5, sub-class implements the abstract methods of the parent class, you need to add the override keyword.

6, if all the abstract methods abstract class subclass is not abstract class, subclass must override the parent class abstract class.

 

Third, the difference between abstract classes and interfaces

 

Same point

  1 can not be instantiated;

  2. The method declaration contains unrealized

  3. The derived class must implement method is not implemented, the abstract class is abstract methods, interfaces are all members (not just the method includes the other members)

 

difference

 

  1. The interface can be multiple inheritance, abstract classes can not implement multiple inheritance.

  2. The interface can only define abstract rules, an abstract class can not only define abstract rules also provide members have achieved

  3. Interface Specification is a set of behaviors, abstract class is a class incomplete, it focuses on the concept of group

  4. The interface supports callback, callback abstract class can not be achieved, because inheritance is not supported.

  The interface contains only the methods, properties, indexers, event signature, but can not define fields and methods comprising achieve abstract class defines properties, fields, implemented method comprising

  6. The interface may be applied to the value and reference types, the abstract class only act on the reference type (e.g.: Struct interfaces can inherit)

  7. The abstract class should be used primarily close objects, and the interface is adapted to provide common functionality unrelated to the class

  8. Can-Do focus on the interface type of relationship, the abstract class type emphasis on IS-A relationship.

  9. Multi-Interface defines behavior of the object, multiple abstract class defines the properties of the object.

  10. If you want to design small, concise function block, the interface is used, if the functional unit to be designed large, the use of the abstract class

  11. The interface corresponds to an important principle of object-oriented programming ideas: Oriented Programming Interface

Guess you like

Origin www.cnblogs.com/frank0812/p/11281648.html