Abstract, interface, Virtual, override, New Brief summary and regularization

interfaceAbstract

The most essential difference between an interface and an abstract class: an abstract class is an incomplete class, an abstraction of objects, while an interface is a behavioral specification.

Classes can inherit multiple interfaces; class-to-class inheritance can only inherit one.

An abstract class must provide implementations for all interface members. An abstract class implementing an interface can map interface methods onto abstract methods.

 


Virtual and override
decoration: The
virtual keyword is used to decorate method, property, indexer or event declarations and make them available to be overridden in derived classes.
The abstract modifier can be used on classes, methods, properties, indexers and events. An abstract method can only be declared and defined in an abstract class.

Implementation:
abstract , there cannot be a concrete implementation, it must be implemented with override in the derived class.
virtual , which can be implemented or not.

Because an abstract method declaration does not provide the actual implementation, there is no method body; the method declaration simply ends with a semicolon and does not have curly braces ({ }) after the signature. For example: public abstract void MyMethod();

 

Use of override and new  in derived classes

Both can be used for methods, properties, indexers and events.

Methods in the base class, if overridden by derived classes are allowed , must increase virtual .

The override keyword overrides the base virtual method and uses the base keyword to call the base class method.

The new keyword hides virtual methods in base classes.

 

 

Knowledge point reference:
https://www.cnblogs.com/zyj649261718/p/6256327.html
https://msdn.microsoft.com/zh-cn/library/6fawty39(v=vs.100).aspx

Reference plaintext:
Both virtual and abstract are used to modify the parent class. By overriding the definition of the parent class, the child class can be redefined.
(1) Virtually modified methods must be implemented (even if only a pair of braces are added), while abstract modified methods must not be implemented.
(2) virtual can be overridden by subclasses, while abstract must be overridden by subclasses.
(3) If a class member is modified by abstract, abstract must be added before the class, because only abstract classes can have abstract methods.
(4) An instance of the abstract class cannot be created, it can only be inherited and cannot be instantiated.

 

Guess you like

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