Language: EN

csharp-herencia

What is and how to use inheritance in C#

Class inheritance 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 it inherits 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).

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 Dog class inherits from the Animal class
  • This means that Dog has access to the methods of Animal, such as Eat
  • In addition to its own methods like Bark.

Method overriding

Derived classes can override methods from 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 MakeSound method from 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 from 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 to call 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 object substitution while maintaining 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 either a Dog or a Cat
  • The appropriate MakeSound method is invoked based on the type of instance (at runtime)