both virtual and abstract methods can be overridden by derived classes, what is the difference between them?
1. The virtual method must have an implementation section and provide options for the derived class to override the method. An abstract method does not provide an implementation part, and an abstract method is a method that enforces the override of a derived class, otherwise the derived class cannot be instantiated
2. Abstract methods can only be declared in abstract classes, and abstract methods must be overridden in derived classes. Virtual methods are not and do not have to be rewritten. In fact, if a class contains an abstract method, the class is also abstract and must be declared as abstract
3. An abstract method must be overridden in a derived class, similar to an interface, which is not necessary for a virtual method. Abstract methods cannot declare method bodies, and virtual methods can. Classes that contain abstract methods cannot be instantiated, and classes that contain virtual methods can be instantiated
Virtual method
public class Animal
{
public virtual void Sleep () {}
public virtual void Eat () {}
}
What are the differences and linkages between virtual and abstract methods?
1, abstract method only declaration does not implement code, you need to implement in the subclass, the virtual method has the declaration and implementation Code, and can be overridden in subclasses, or can not override the default implementation of using the parent class.
2. Abstract classes cannot be instantiated (not new), only derived classes that implement all abstract methods are instantiated, and classes containing virtual methods can be instantiated.
3, virtual method and polymorphism are closely related, virtual method allows derived classes to completely or partially rewrite the method of the class, need to write the method body. Abstract method is just a definition, there is no method body, that is, no {}, do not write content in it.
4, the abstract method is the virtual method two is similar to the point is to rewrite with override
the difference between non-abstract class and interface
1. Non-abstract classes can have member variables, constants, and constructors, and there cannot be constants, fields, operators, constructors, or destructors in an interface, and cannot contain any static members.
2, a non-abstract class can inherit multiple interfaces, but only inherit a class;
3, the non-abstract class can define the concrete method and the implementation, but the interface only defines the method to have no concrete realization;
4, interface members must be public, and can not be declared as virtual or static, but not abstract classes are not these restrictions.
5, non-abstract classes can be instantiated, and interfaces are not. In general, the content of non-abstract classes is much richer than interfaces, and non-abstract classes can have most of the constituent elements of object-oriented. However, the interface is more flexible than non-abstract classes, the two are not used in the conflict, depending on the situation when used
C # classes, interfaces, virtual methods, and abstract methods