C# Inheritance
Last modified: July 11, 2021In C#, it is possible to inherit fields and methods from one class to another and we used the :
symbol to inherit. Derived Class is the child and Base Class is the parent C# class.
In the example below, we have the Animal
class (parent) and the Cat
class
public class Animal
{
public bool vegetarian;
}
public class Cat : Animal
{
public void animalSound()
{
Console.WriteLine("Mewo");
}
}
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
cat.vegetarian = false;
cat.animalSound();
}
}