Inheritance of classes in C# is a mechanism that allows one class to derive from another class. This means it will “inherit” all its fields, properties, and methods.
The class from which inheritance is taken is known as the base class (or parent class). On the other hand, the class that inherits is called the derived class (or child class).
If you want to learn more, check out the Object-Oriented Programming Course
Implementation of Inheritance
Inheritance in C# is implemented using a colon (:) followed by the name of the base class.
public class BaseClass
{
// Members of the base class
}
public class DerivedClass : BaseClass
{
// Additional members of the derived class
}
Let’s see it better with a simple example,
public class Animal
{
public void Eat()
{
Console.WriteLine("The animal is eating.");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("The dog is barking.");
}
}
// Usage
Dog myDog = new Dog();
myDog.Eat(); // Inherited from Animal
myDog.Bark(); // Defined in Dog
In this example,
- The
Dogclass inherits from theAnimalclass - This means
Doghas access to the methods ofAnimal, such asEat - In addition to its own methods like
Bark.
Method Overriding
Derived classes can override methods from the base class using the virtual keyword in the base class and override in the derived class.
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound.");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog barks.");
}
}
// Usage
Animal myAnimal = new Animal();
myAnimal.MakeSound(); // Output: The animal makes a sound.
Dog myDog = new Dog();
myDog.MakeSound(); // Output: The dog barks.
In this example, Dog overrides the MakeSound method of Animal to provide a specific implementation.
The base Keyword
The base keyword is used to access members of the base class from a derived class (such as constructors, methods, and properties of the base class).
public class Animal
{
public string Name { get; set; }
public Animal(string name)
{
Name = name;
}
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound.");
}
}
public class Dog : Animal
{
public Dog(string name) : base(name)
{
}
public override void MakeSound()
{
base.MakeSound(); // Invokes the method of the base class
Console.WriteLine("The dog barks.");
}
}
// Usage
Dog myDog = new Dog("Fido");
myDog.MakeSound();
// Output:
// The animal makes a sound.
// The dog barks.
Constructors in Derived Classes
When an instance of a derived class is created, the constructor of the base class is automatically invoked before the constructor of the derived class.
You can explicitly specify which base class constructor should be called using the base keyword.
public class Animal
{
public string Name { get; set; }
public Animal(string name)
{
Name = name;
}
}
public class Dog : Animal
{
public string Breed { get; set; }
public Dog(string name, string breed) : base(name)
{
Breed = breed;
}
}
// Usage
Dog myDog = new Dog("Fido", "Labrador");
Console.WriteLine($"Name: {myDog.Name}, Breed: {myDog.Breed}");
// Output: Name: Fido, Breed: Labrador
Inheritance and Polymorphism
Polymorphism is another key feature of OOP that complements inheritance. It allows a derived class to be treated as an instance of its base class. This allows objects to be substituted while still maintaining the overridden methods.
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("The cat meows.");
}
}
// Usage
Animal myAnimal;
myAnimal = new Dog("Fido");
myAnimal.MakeSound(); // Output: The dog barks.
myAnimal = new Cat("Mishi");
myAnimal.MakeSound(); // Output: The cat meows.
In this example,
myAnimalcan refer to aDogor aCat- The appropriate
MakeSoundmethod is invoked based on the instance type (at runtime)
