Language: EN

csharp-herencia

What is and how to use inheritance in C#

Class inheritance is a mechanism that allows a class to derive from another class, inheriting its fields, properties, and methods.

The class from which it inherits is known as the base class (or parent class), while the inheriting class 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 leer más ⯈

Implementing inheritance in C#

Inheritance in C# is implemented using the syntax of 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, which 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 of the base class using the virtual keyword in the base class and the override keyword 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.

Base keyword

The base keyword is used to access members of the base class from a derived class. This is useful for invoking 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.

It can be explicitly specified 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, allowing for object substitution and invocation of overridden methods at runtime.

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 a Dog or a Cat, and will call the appropriate MakeSound method based on the type of instance at runtime.