C# Method Overriding - c# - c# tutorial - c# net




What is method overriding in C# ?

  • If derived class defines same method as defined in its base class, it is known as method overriding in C#. It is used to achieve runtime polymorphism.
  • It enables you to provide specific implementation of the method which is already provided by its base class.
  • To perform method overriding in C#, you need to use virtual keyword with base class method and override keyword with derived class method.
Method Overloading and Overriding

Method Overloading and Overriding

C# Method Overriding Example

  • Let's see a simple example of method overriding in C#. In this example, we are overriding the eat() method by the help of override keyword.
using System;  
public class Animal{  
    public virtual void eat(){  
        Console.WriteLine("Eating...");  
    }  
}  
public class Dog: Animal  
{  
    public override void eat()  
    {  
        Console.WriteLine("Eating bread...");  
    }  
}  
public class TestOverriding  
{  
    public static void Main()  
    {  
        Dog d = new Dog();  
        d.eat();  
    }  
}  
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Eating bread...


Related Searches to C# Method Overriding

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×