SoFunction
Updated on 2025-03-02

Detailed explanation of virtual methods, abstract classes, and interfaces based on polymorphism

Virtual method:

1. Add the virtual keyword before the return value of the parent class method and mark it as a virtual method, indicating that this method can be rewritten by the subclass.

2. The virtual method must have a method body, and there can be no content in the method body.

3. Subclasses can selectively override virtual methods according to requirements. If you need to overwrite, add the override keyword before the return value of the subclass method.

4. When a subclass overrides virtual methods, it can selectively call the method in the parent class using the base keyword according to the needs.

The syntax format of virtual method is as follows:

public class Father
{
 public virtual void Do()
 {
  //.....
 }
}
public class Son : Father
{
 public override void Do()
 {
  ();//Selectively call it.  //....Code body }
}

Abstract class:

1. Add abstract to the keyword class that defines the class to indicate that this class is an abstract class. After the subclass inherits the abstract class, it overrides all abstract methods in the parent class using the override keyword.

2. There are not necessarily abstract methods in abstract classes, but abstract methods must exist in abstract classes. Abstract methods also need to be modified with the abstract keyword.

3. An abstract method has no method body, and the abstract method must be implemented in a subclass.

4. Abstract classes cannot be instantiated, but they can have constructors. Because there are abstract methods (no method body) in abstract classes, if the abstract class is instantiated, then it is meaningless for the abstract class objects to call these abstract methods without method body, so they cannot be instantiated.

The syntax format of abstract classes and abstract methods is as follows:

public abstract class Father//Abstract Class{
 public abstract void Do();//Abstract Method}
public class Son : Father
{
 public override void Do()
 {
  //...
 }
}

Interface:When all methods in an abstract class are abstract methods, they can be expressed in the form of an interface.

1. The interface is defined using the interface keyword, without the class keyword, and the interface name is generally "IXxxx".

2. The interface cannot contain fields, but can contain attributes (automatic attributes).

3. The methods defined in the interface cannot have a method body. They are all abstract methods, but they do not need to be modified with the abstract keyword. Therefore, the interface cannot be instantiated and cannot have a constructor.

4. Members in the interface are not allowed to add access modifiers, and they are all public by default.

5. A class can implement multiple interfaces, and the implemented interfaces are separated by commas; an interface can also inherit multiple interfaces, and the interfaces must also be separated by commas; when an interface implements an interface, if there are the same methods in the two interfaces, the new keyword can be used to hide the methods in the parent interface.

6. The inheritor must implement all methods in the interface.

The interface syntax format is as follows:

interface IFather
{
 void Do();
}

interface IMother:IFather
{
 new void Do();//Hide the parent interface with the same name method void Do1();
}

public class Son : IFather,IMother
{
 public void Do()
 {
  //...... 
 }

 public void Do1()
 {
  //.....
 }
}

The above detailed explanation of virtual methods, abstract classes, and interfaces based on polymorphism is all the content I share with you. I hope you can give you a reference and I hope you can support me more.