TOC

This article is currently in the process of being translated into French (~44% done).

Classes:

Inheritance

L'un des aspects essentiels de la Programmation Orientée Objet (POO), qui est le concept sur lequel C# est construit, est l'héritage : la capacité à créer des classes qui héritent de plusieurs aspects de leurs classes parent. Le framework .NET tout entier est construit sur ce principe, ce qui mène au résultat "tout est objet". Même un simple nombre est une instance d'une classe qui hérite de la classe System.Object, bien que .NET nous aide un peu, ainsi vous pouvez assigner directement un nombre au lieu d'avoir à créer une nouvelle instance de la classe integer (par exemple).

Ce sujet peut être un peu difficile à appréhender mais parfois c'est plus facile avec des exemples. Donc commençons par un très simple:

public class Animal
{
    public void Greet()
    {
        Console.WriteLine("Hello, I'm some sort of animal!");
    }
}

public class Dog : Animal
{

}

Tout d'abord, on définit une classe Animal, avec une méthode très simple pour afficher un message. Ensuite, on définit une classe Dog and avec le signe 2 points ":", on dit à C# que la classe Dog doit hériter de la classe Animal. Ce qui est beau dans tout ceci est que cela a un sens dans le monde réel également - un Dog (chien) est assurément un Animal. Essayons d'utiliser les classes:

Animal animal = new Animal();
animal.Greet();
Dog dog = new Dog();
dog.Greet();

If you run this example, you will notice that even though we have not defined a Greet() method for the Dog class, it still knows how to greet us, because it inherits this method from the Animal class. However, this greeting is a bit anonymous, so let's customize it when we know which animal it is:

public class Animal
{
    public virtual void Greet()
    {
        Console.WriteLine("Hello, I'm some sort of animal!");
    }
}

public class Dog : Animal
{
    public override void Greet()
    {
        Console.WriteLine("Hello, I'm a dog!");
    }
}

Besides the added method on the Dog class, you should notice two things: I have added the virtual keyword to the method on the Animal class, and on the Dog class, I use the override keyword.

In C#, you are not allowed to override a member of a class unless it's marked as virtual. If you want to, you can still access the inherited method, even when you override it, using the base keyword.

public override void Greet()
{
    base.Greet();
    Console.WriteLine("Yes I am - a dog!");
}

Methods are not the only thing to get inherited, though. In fact, pretty much all class members will be inherited, including fields and properties. Just remember the rules of visibility, as discussed in a previous chapter.

Inheritance is not only from one class to another - you can have a whole hierarchy of classes, which inherits from eachother. For instance, we could create a Puppy class, which inherits from our Dog class, which in turn inherits from the Animal class. What you can't do in C#, is to let one class inherit from several other classes at the same time. Multiple inheritance, as it's called, is not supported by C#.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!