Because subclasses inherit class parent classes, subclasses have the same behavior, but the behavior of subclasses sometimes needs to be different from each other
Subclasses need to override the parent class's methods to implement the subclass-specific behavior, which is called Polymorphism in C #.
Polymorphism is the same type of object calling the same method but showing a different phenomenon
Implementing overriding methods using the virtual and override keywords
Can be overridden by a derived class only if the base class member is declared as virtual and abstract
If a subclass wants to change the implementation behavior of a virtual method, it must use the Override keyword
The result of the final execution is:
But there is a problem with this code
It is
Animal animal=new Animal ();
This kind of remark is meaningless, because the base class here is to provide a public member for the subclass
Then we can create the base class as an abstract class and take advantage of the abstract keyword to prevent this from happening.
Prevent derived classes from overriding virtual members
Use the sealed keyword to achieve
If you have a class that inherits horse and tries to override the Voice method, you receive an error message
Hide base class members with new members
Use the New keyword to implement
C # Object-oriented polymorphism