TOC

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

Clase:

Inheritance

Unul dintre aspectele cheie ale Programării Orientate pe Obiect, care reprezintă conceptul care stă la baza C#, este moștenirea, abilitatea de a crea clase care moștenesc anumite aspecte de la o clasă părinte (clasă de bază). Întregul framework .NET este conceput pe baza acestui concept, unde "totul este un obiect". Chiar și un simplu număr este o instanță a unei clase care moștenește clasa System.Object, dar .NET te ajută un pic, iar tu poți atribui un număr direct, în loc să creezi o nouă instanță. Exemplu: clasa integer.

Acest subiect poate fi dificil de inteles, dar uneori exemplele sunt de ajutor, deci sa incepem cu unul simplu:

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

public class Dog : Animal
{

}

First, we define an Animal class, with a simple method to output a greeting. Then we define a Dog class, and with a colon, we tell C# that the Dog class should inherit from the Animal class. The beautiful thing about this is that it makes sense in the real world as well - a Dog is, obviously, an Animal. Let's try using the 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!