SoFunction
Updated on 2025-03-08

Introduction to C# and advanced programming of C# polymorphism

Methods marked with virtual keywords are called virtual methods. If the subclass wants to change the implementation details of the virtual method, it must use the override keyword.
Abstract Class
abstract keywords
Prevents the creation of meaningless basic classes, use abstract keyword to create abstract base classes, prevents it from being instantiated
Use the abstract keyword to create abstract methods, forcing each subclass to override a specific method, and the abstract member does not provide any implementation.
(Note: Abstract methods can only be defined in abstract classes. If this is not the case, you will receive a compiler error)
Member projection
If the members defined by the derived class are consistent with the members defined in the fern, the derived class projectes the version of the parent class.
For example: If we create a subclass from a class that we are not created ourselves, it is very likely that this will happen (the same method name appears in the base class and the subclass.)
Solution:
Update the parent version with the override keyword, but if we have no access to the code that defines the base class, we cannot modify the method to a virtual method.
We can add new keywords to members of derived types. This can indicate that the implementation of the derived type is deliberately designed to hide the version of the parent class.

Encapsulation, inheritance, and polymorphism, the three major characteristics of object-oriented are relatively easy to understand, but understanding polymorphism, especially in-depth understanding, may be difficult for beginners. I have always believed that the best way to learn OO is to combine it with practice, and the application of encapsulation and inheritance in actual work can be seen everywhere, but what about polymorphism? Perhaps not necessarily, it may not be possible to use it inadvertently and will not correspond to the word "polymorphism". I’m here to attract attention. Everyone discusses my personal abilities are limited, so please correct me if there are any shortcomings.

I have seen a similar question before: If the examiner asked you to describe polymorphism in one sentence and be as refined as possible during the interview, how would you answer? Of course, there are many answers, and everyone's understanding and expression are different, but I tend to describe it like this: different objects implemented through inheritance call the same method and show different behaviors, which is called polymorphism.
Copy the codeThe code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware)/-->public class Animal
{
public virtual void Eat()
{
("Animal eat");
}
}

public class Cat : Animal
{
public override void Eat()
{
("Cat eat");
}
}

public class Dog : Animal
{
public override void Eat()
{
("Dog eat");
}
}

class Tester
{
static void Main(string[] args)
{
Animal[] animals = new Animal[3];

animals[0] = new Animal();
animals[1] = new Cat();
animals[2] = new Dog();

for (int i = 0; i < 3; i++)
{
animals[i].Eat();
}
}
}

The output is as follows:
Animal eat...
Cat eat...
Dog eat...

In the above example, through inheritance, different objects in the Animal object array exhibit different behaviors when calling the Eat() method.
The implementation of polymorphism seems very simple. It is not easy to fully understand and flexibly use the polymorphism mechanism of C#. There are many things to pay attention to.

Usage
Let’s take a look at the following example first.
Example 2:
Copy the codeThe code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware)/-->public class Animal
{
public virtual void Eat()
{
("Animal eat");
}
}

public class Cat : Animal
{
public new void Eat()
{
("Cat eat");
}
}

class Tester
{
static void Main(string[] args)
{
Animal a = new Animal();
();

Animal ac = new Cat();
();

Cat c = new Cat();
();
}
}

The running result is:
Animal eat...
Animal eat...
Cat eat...

It can be seen that when the Eat() method of the derived class Cat is modified with new, after the Cat object is converted into an Animal object, the Eat() method in the Animal class is called. In fact, it can be understood that after using the new keyword, the Eat() method in Cat and the Eat() method in Animal are two unrelated methods, but their names happen to be the same. Therefore, the Eat() method in the Animal class does not work or does not use virtual modification, regardless of access permissions, or does not have any impact on the Eat() method of Cat (just because the new keyword is used, if the Cat class does not use inheriting the Eat() method from the Animal class, the compiler will output a warning).

I think this is what the designers intentionally designed because sometimes we want to achieve this effect. Strictly speaking, it cannot be said that polymorphism is achieved by using new, it can only be said that polymorphism effect happens to be achieved at certain specific times.

Implement polymorphism
True polymorphism is achieved using override. Looking back at the previous example 1, in the base class Animal, the method Eat() is marked as a virtual method with virtual, and then the derived classes Cat and Dog are modified and override and rewrite them. It is very simple to implement polymorphism. It should be noted that to modify a method in a class with override, the class must inherit a corresponding virtual method modified from the parent class, otherwise the compiler will report an error.

It seems that I have already said it all, and there is another question, I don’t know if you want to. That is how to achieve polymorphism in multi-layer inheritance. For example, class A is a base class, and there is a virtual method method() (virtual modification). Class B inherits from class A and rewrites method() (override modification). Now class C inherits from class B. Can we continue to rewrite method() and implement polymorphism? Look at the example below.

Example 3:
Copy the codeThe code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware)/-->public class Animal
{
public virtual void Eat()
{
("Animal eat");
}
}

public class Dog : Animal
{
public override void Eat()
{
("Dog eat");
}
}

public class WolfDog : Dog
{
public override void Eat()
{
("WolfDog eat");
}
}

class Tester
{
static void Main(string[] args)
{
Animal[] animals = new Animal[3];

animals[0] = new Animal();
animals[1] = new Dog();
animals[2] = new WolfDog();

for (int i = 0; i < 3; i++)
{
animals[i].Eat();
}
}
}

The running result is:
Animal eat...
Dog eat...
WolfDog eat...

In the example above, the class Dog inherits from the class Animal, and the method Eat() is rewritten, and the class WolfDog inherits from Dog, and the Eat() method is rewritten again, and polymorphism is implemented well. No matter how many layers you inherit, you can continue to rewrite the methods that have been rewritten in the parent class in the subclass. That is, if the parent class method is modified with override, and if the subclass inherits the method, you can also use override to modify it. This is how the polymorphism in multi-layer inheritance is implemented. To terminate this rewrite, you only need to modify the method with the sealed keyword.

3. Abstract-override implements polymorphism
Let’s first discuss the abstract method of using abstract modification. Abstract methods only define methods but are not implemented. If a class contains abstract methods, then the class must also be declared as an abstract class with abstract. An abstract class cannot be instantiated. For abstract methods in a class, you can rewrite them with override in their derived classes. If they are not rewrite, their derived classes must also be declared as abstract classes. Look at the example below.
Example 4:
Copy the codeThe code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware)/--> public abstract class Animal
{
  public abstract void Eat();
}

public class Cat : Animal
{
public override void Eat()
{
("Cat eat");
}
}

public class Dog : Animal
{
public override void Eat()
{
("Dog eat");
}
}

public class WolfDog : Dog
{
public override void Eat()
{
("Wolfdog eat");
}
}

class Tester
{
static void Main(string[] args)
{
Animal[] animals = new Animal[3];

animals[0] = new Cat();
animals[1] = new Dog();
animals[2] = new WolfDog();

for (int i = 0; i < ; i++)
{
animals[i].Eat();
}
}
}

The running result is:
Cat eat...
Dog eat...
Wolfdog eat...
As can be seen from the above, polymorphism can be implemented the same as virtual-override by using abstract-override, including multi-layer inheritance. The difference is that classes containing virtual methods can be instantiated, while classes containing abstract methods cannot be instantiated.
The above are some of my shallow understanding of polymorphism in C#. If there are any mistakes, please criticize and correct me!