SoFunction
Updated on 2025-03-06

The difference between Abstract method and Virtual method in C#

Introduction:

Abstract and Virtual in C# are more easily confused, both are related to inheritance and involve the use of override. Virtual can be rewritten by subclasses, while abstract must be rewritten by subclasses. The virtual modification method must be implemented (even if it is just adding a pair of braces), while the abstract modification method must not be implemented. They have one thing in common: if used to modify methods, public must be added before it, otherwise a compilation error will occur: virtual methods or abstract methods cannot be private. After all, adding virtual or abstract is to redefine the subclass, while private members cannot be accessed by subclasses. Let’s discuss the differences between the two below:

1. Virtual method (virtual method)

The virtual keyword is used to modify methods in the base class. There are two situations in which virtual use:
Case 1: The virtual method is defined in the base class, but the virtual method is not overridden in the derived class. Then in the call to the derived class instance, the virtual method uses the method defined by the base class.
Case 2: The virtual method is defined in the base class, and then the method is override in the derived class. Then in the call to the derived class instance, the virtual method uses the derived overridden method.

Virtual can be rewritten by subclasses:

    class BaseTest1
    {
       public virtual void fun() { }// There must be implementation    }
    class DeriveTest1:BaseTest1
    {
        //public override void fun() { }
    }

The virtual modification method must be implemented (even if you just add a pair of braces):

    public class Test1
    {
        public virtual void fun1(){}
    }

2. Abstract method (abstract method)

abstract keywordsOnly use to modify methods in abstract classes, and there is no specific implementation. The implementation of abstract methods must be implemented using the override keyword in the derived class.
The most essential difference between interfaces and abstract classes:An abstract class is an incomplete class, an abstraction of objects, while an interface is a behavioral norm.

abstract must be rewritten by subclasses:

    abstract class BaseTest2
    {
        public abstract void fun();
    }
    class DeriveTest2 : BaseTest2
    {
        //public override void fun();Error 1: Not implemented        //public void fun() { } Error 2: Override was not added during rewriting        //override void fun() { }Error 3: Virtual members or abstract members cannot be private (as long as virtual members or abstract members are declared in the parent class, this restriction must be added even if it is inherited)        public override void fun() { }//If the method is overridden; Error: "A.DeriveTest2" does not implement the inherited abstract member "A.()"
    }

The abstract modification method must not be implemented:

    public abstract class Test2
    {
        public abstract void fun2() ;
    }

3. Keywords

Static:When a method is declared as Static, this method is a static method, and the compiler will retain the implementation of this method at compile time. That is to say, this method belongs to a class, but does not belong to any member. Regardless of whether the instance of this class exists or not, they will exist. Just like the entry function Static void Main, because it is a static function, it can be called directly.

Virtua:When a method is declared as Virtual, it is a virtual method, and it does not exist in real memory until you declare an instance of a class using ClassName variable = new ClassName();. This keyword is very commonly used in class inheritance and is used to provide polymorphic support for class methods.

overrride: means rewrite This class is inherited from the Shape class
virtual, abstract tells other classes that want to inherit from him. You can rewrite my method or property, otherwise it will not be allowed.
abstract: The use of abstract method declaration is a method that must be overridden by the derived class. Abstract classes are used to be inherited; they can be regarded as virtual methods without implementation bodies; if the class contains abstract methods, then the class must be defined as an abstract class, regardless of whether other general methods are included; abstract classes cannot have entities.

The above is the difference between Abstract method and Virtual method in C# that the editor introduced to you. I hope it will be helpful to you. Thank you very much for your support for my website!