TOC

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

Classes:

Inheritance

واحد من المفاتيح المطلقة للبرمجة الغرضية الموجهة (التي بنيت عليها #C خلال بنائها) هو الوراثة وهي قدرة الفئة(الكلاس) على وراثة جميع النواحي من الفئة الأب. جميع أطر NET. مبنية على هذا المبدأ (كل شيء عبارة عن غرض) نتيجة لذلك. حتى أبسط رقم هو جزء من الفئة التي تورث من الفئة System.Object من خلال NET. لتساعدك لتعيين رقم مباشرة بدلاً من إنشاء جزء جديد مثل فئة العدد الصحيح.

هذا الموضوع من الممكن أن يكون صعب الفهم. ولكن المثال سوف يساعد على فهم الموضوع، فلنبدأ بمثال بسيط :

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!