C Sharp Method Overriding

C# Method Overriding

Method overriding in C# is the process where the derived class defines the same method as defined in its base class. To obtain runtime polymorphism, and to provide a specific implementation of the method is already provided by the base class, the method overriding is used. The virtual keyword is used with the base class method and the override keyword is used with the derived class method in C# for method overriding.

Example:

using System;  
public class Flower{  
    public virtual void color(){  
        Console.WriteLine("White Flower!!");  
    }  
}  
public class Lily: Flower  
{  
    public override void color()  
    {  
        Console.WriteLine("White Lily!!");  
    }  
}  
public class Display  
{  
    public static void Main()  
    {  
        Lily f = new Lily();  
        f.color();  
    }  
}

Output:

Explanation:

In the above example, we are displaying the behavior and the use of method overriding in C#. Here, the override keyword is used for overriding the color() method.

Please follow and like us:
Content Protection by DMCA.com