C# abstract class, interface explanation

Difference between interface and abstract class

  1. Interfaces are used for specification and abstract classes are used for commonality.
  2. Only methods, properties, events, and indexers can be declared in an interface. An abstract class can implement methods or define non-static class variables.
  3. An abstract class is a class, so it can only be inherited by a single one, but an interface can implement more than one at a time.
  4. Abstract classes can provide partial implementations of certain methods, interfaces cannot.
  5. An instance of an abstract class is given by its subclasses. An instance of an interface is given by the class that implements the interface.
  6. Add a method to an abstract class, and its subclasses will have this method at the same time. When adding a new method to an interface, the class that implements it needs to be rewritten (this is why an interface is a class specification).
  7. Interface members are defined as public, but members of abstract classes can also be private, protected, internal, or protected internal members (where protected internal members can only be accessed in the application's code or in derived classes) ).
  8. Also an interface cannot contain fields, constructors, destructors, static members or constants.

The difference between Overload and Override

Brief description : Simply put, one is several forms of the same function, and the other is to rewrite the parent class function.

Overloading: Method overloading occurs when a class contains two methods with the same name but different signatures (same method name, different parameter list). Use method overloading to provide methods that perform the same semantically but function differently.

Override: It is used in class inheritance. By overriding the subclass method, the implementation of the virtual method of the parent class can be changed.

the difference:

  1. Method overriding is the relationship between subclasses and parent classes, which is a vertical relationship; method overloading is the relationship between methods in the same class, which is a horizontal relationship.
  2. Overriding can only be related by one method, or only by a pair of methods; overloading of a method is a relationship between multiple methods.
  3. Overrides require the same argument list; overloads require a different argument list.
  4. In the overriding relationship, which method body is called is determined according to the type of the object (the object corresponds to the storage space type); in the overloading relationship, the method body is selected according to the actual parameter list and formal parameter list at the time of invocation.

 Difference between interface and abstract class.

Interfaces are used for specification and abstract classes are used for commonality.
Only methods, properties, events, and indexers can be declared in an interface. An abstract class can implement methods or define non-static class variables.
An abstract class is a class, so it can only be inherited by a single one, but an interface can implement more than one at a time.
Abstract classes can provide partial implementations of certain methods, interfaces cannot.
An instance of an abstract class is given by its subclasses. An instance of an interface is given by the class that implements the interface.
Add a method to an abstract class, and its subclasses will have this method at the same time. When adding a new method to an interface, the class that implements it needs to be rewritten (this is why an interface is a class specification).
Interface members are defined as public, but members of abstract classes can also be private, protected, internal, or protected internal members (where protected internal members can only be accessed in the application's code or in derived classes) ).
Also an interface cannot contain fields, constructors, destructors, static members or constants.

What are the similarities and differences between interfaces and classes in C#.
Exclusive:
Interfaces cannot be instantiated directly.
Interfaces do not contain implementations of methods.
Interfaces can implement multiple inheritance, while classes can only have single inheritance.
Class definitions can be split between different source files.
Same:
Interfaces, classes, and structs can inherit from multiple interfaces.
An interface is like an abstract base class: any non-abstract type that inherits the interface must implement all the members of the interface.
An interface can contain events, indexers, methods, and properties.

A class can implement multiple interfaces.

Article reference: http://www.cnblogs.com/jiajiayuan/archive/2011/09/16/2178462.html


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735218&siteId=291194637