Abstract methods are methods defined in abstract classes that need to be implemented in subclasses. Virtual methods are methods that can be overridden by subclasses, or that can be overridden.

directory

What are abstract methods and virtual methods?

The difference between abstract and virtual methods

Give examples to analyze the differences in methods

Abstract methods

Virtual method


Hello! I’m grey Ape!

Recently in the development, I got the term virtual method, because it was used in the abstract class at that time, and the use of abstract method and virtual method is so slightly similar, I have known virtual method before, but the concept is vague, so today here to record the difference between abstract method and virtual method.

 

What are abstract methods and virtual methods?

Generally speaking, an abstract method is a method defined in an abstract class that needs to be implemented in a subclass.

Virtual methods are methods that can be overridden by subclasses, or that can be overridden.

 

The difference between abstract and virtual methods

Abstract methods: have only the method name, but no method body. The method name must be defined by the abstract keyword. It must be declared in the abstract class.

Virtual methods: have a method name and a method body, and are preceded by the virtual keyword. They do not have to be declared in an abstract class. They can be declared in any unsealed class, but when a subclass inherits a parent class, the virtual methods that exist in the parent class may or may not be overridden.

For more information on abstract classes, you can refer to my article “Are Abstract classes abstracted? Just look at the examples!”

 

Give examples to analyze the differences in methods

To illustrate the different uses of an abstract method and a virtual method, the following code is written in C#, similar to Java and other languages.

 

Abstract methods

For example, there is a base class “animal” and two subclasses “birds” and “chickens.” Both classes have common and unique properties and methods.

For example, chickens and birds can sing, but their calls are different.

Rooster crow: cluck

Bird call: Chick chick

We can then define an abstract method called “call” that constrains “birds” and “chickens” in the base “animal” class. Let the two classes implement the method separately.

The code for the abstract method is as follows:

Define an abstract class “animal class” to implement an abstract method

// Define the abstract class "animal class"
    abstract class Animal
    {
        // Define the abstract method "call"
        abstract public void cry();
    }
Copy the code

Define “bird” and inherit from “animal class” to implement the abstract method in “animal class”.

// Subclass "bird" and inherit from "animal"
 class Fowl : Animal
    {
        // Implement the abstract method cry in "animal class"
        public override void cry()
        {
            Console.WriteLine("Bird's Call: Chick chick."); }}Copy the code

Define “chicken class” and inherit from “animal class” to implement abstract methods in “animal class”.

 // Define "chicken" and inherit from "animal"
    class Chicken : Animal
    {
        // Implement the abstract method cry in "animal class"
        public override void cry()
        {
            Console.WriteLine("Cock crow: cluck."); }}Copy the code

Call “bird” and “chicken” respectively in the main class to achieve the call method

class Program
    {
        static void Main(string[] args)
        {           
            // Create a new object to instantiate "bird"
            Fowl fowl = new Fowl();
            // Create a new object to instantiate "chicken"
            Chicken chicken = new Chicken();

            fowl.cry();     // Implement the "bird" call method
            chicken.cry();  // Implement the call method in "chicken"}}Copy the code

Running results:

 

Virtual method

For example, chickens and birds can fly, and both flapping their wings, but the birds fly very high and the chickens fly very low. This is the method of flight. “chickens” and “birds” have both common characteristics and different characteristics.

Then we can define the flying method as virtual method, rewrite this method in “chicken” and “bird” respectively, but retain the common feature of this method “flapping wings”, add its own unique feature “flying height”.

The code for the virtual method is as follows:

Because virtual methods do not have to be declared in abstract classes, but can be declared in any unsealed class, the action of flight is declared in the base class “animal class.”

// Define base class "animal class"
    class Animal
    {
        // Define the abstract method "call"
        public void fly()
        {
            Console.WriteLine("Flapping your wings to fly."); }}Copy the code

Rewrite the flight method in “birds” to add class-specific features:

 // Subclass "bird" and inherit from "animal"
    class Fowl : Animal
    {
        // Override the fly method in "animal" and hide the fly method in the base class with the bew keyword
        public new void fly()
        {
            Console.WriteLine("Flapping your wings to fly.");
            Console.WriteLine("Birds fly high."); }}Copy the code

Rewrite the flight method in “chicken” and add the characteristics unique to the class:

// Define "chicken" and inherit from "animal"
    class Chicken : Animal
    {
        // Override the fly method in "animal" and hide the fly method in the base class with the bew keyword
        public new void fly()
        {
            Console.WriteLine("Flapping your wings to fly.");
            Console.WriteLine("The chicken flies very low."); }}Copy the code

Call “bird” and “chicken” respectively in the main class to implement the flight method:

   class Program
    {
        static void Main(string[] args)
        {         
            // Create a new object to instantiate "bird"
            Fowl fowl = new Fowl();
            // Create a new object to instantiate "chicken"
            Chicken chicken = new Chicken();

            fowl.fly();     // Implement the flying method in "bird"
            chicken.fly();  // Implement the "chicken" flying method}}Copy the code

Running results:

Feel good remember to like attention yo!

Big bad Wolf accompany you to progress together!