Member-only story

Overriding in C#

Shahzad Aslam
5 min readMar 31, 2024

--

when you have a derive class (child class) and base class (parent classs). You can implement the base class (parent class) methods in derived class (child class). In base class we use vrtual keyword for defining the method. We use override keyword for implementing the base class method in derive class. Overriding is feature of Object Oriented Programming .

base class (Parent Class):

The base class (Parent) class is that class where we will have intend to implement the methods that will be enable to derive in child class. You must use virtual keyword to define the base class method.

Example

In below example we have base class (parent class) Fruit. This class has virtual method Eat. This class also have two more virtual method Taste and Color.

base class (parent class)
public class Fruit
{
public virtual void Eat()
{
Console.WriteLine("Fruit is for eating.");
}
public virtual void Taste()
{
Console.WriteLine("Fruit has sweet taste.");
}
public virtual void Color()
{
Console.WriteLine("Fruit has color.");
}
}

Derived Class (Child Class):

In the base class, you define a method that you intend to allow derived classes to override. You use the override keyword to mark the method as overridable.

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet