Class inheritance in C# is a mechanism that allows a class to derive from another class. This means it will “inherit” all its fields, properties, and methods.
The class from which it is inherited 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 about Inheritance between classes
check out the Object-Oriented Programming Course read more
Implementation of inheritance in C#
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
}
Basic 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 class
Dog
inherits from the classAnimal
- This means that
Dog
has access to the methods ofAnimal
, such asEat
- In addition to its own methods like
Bark
.
Method overriding
Derived classes can override methods of the base class using the keywords virtual
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 method MakeSound
of Animal
to provide a specific implementation.
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 constructor of the base class 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 for object substitution, 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,
myAnimal
can refer to aDog
or aCat
- The appropriate
MakeSound
method is invoked based on the type of instance (at runtime)