TOC

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

Clase:

Abstract classes

Clasele abstracte, marcate de cuvântul cheie abstract în definiția clasei sunt în general utilizate pentru a defini o clasă de bază într-o ierarhie. Ce este special în legătură cu ele, este faptul că nu poate fi creată o instanță a lor - dacă încerci, compilatorul va semnala o eroare. În schimb, acestea trebuie subclasate, așa cum am învățat în capitolul despre moștenire, iar mai apoi trebuie creată o instanță a subclasei. Deci când avem nevoie de o clasă abstractă? Depinde de ceea ce ai de făcut.

To be honest, you can go a long way without needing an abstract class, but they are great for specific things, like frameworks, which is why you will find quite a bit of abstract classes within the .NET framework it self. A good rule of thumb is that the name actually makes really good sense - abstract classes are very often, if not always, used to describe something abstract, something that is more of a concept than a real thing.

In this example, we will create a base class for four legged animals and then create a Dog class, which inherits from it, like this:

namespace AbstractClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Console.WriteLine(dog.Describe());
            Console.ReadKey();
        }
    }

    abstract class FourLeggedAnimal
    {
        public virtual string Describe()
        {
            return "Not much is known about this four legged animal!";
        }
    }

    class Dog : FourLeggedAnimal
    {

    }
}

If you compare it with the examples in the chapter about inheritance, you won't see a big difference. In fact, the abstract keyword in front of the FourLeggedAnimal definition is the biggest difference. As you can see, we create a new instance of the Dog class and then call the inherited Describe() method from the FourLeggedAnimal class. Now try creating an instance of the FourLeggedAnimal class instead:

FourLeggedAnimal someAnimal = new FourLeggedAnimal();

You will get this fine compiler error:

Cannot create an instance of the abstract class or interface 'AbstractClasses.FourLeggedAnimal'

Now, as you can see, we just inherited the Describe() method, but it isn't very useful in it's current form, for our Dog class. Let's override it:

class Dog : FourLeggedAnimal
{
    public override string Describe()
    {
        return "This four legged animal is a Dog!";
    }
}

In this case, we do a complete override, but in some cases, you might want to use the behavior from the base class in addition to new functionality. This can be done by using the base keyword, which refers to the class we inherit from:

abstract class FourLeggedAnimal
{
    public virtual string Describe()
    {
        return "This animal has four legs.";
    }
}


class Dog : FourLeggedAnimal
{
    public override string Describe()
    {
        string result = base.Describe();
        result += " In fact, it's a dog!";
        return result;
    }
}

Now obviously, you can create other subclasses of the FourLeggedAnimal class - perhaps a cat or a lion? In the next chapter, we will do a more advanced example and introduce abstract methods as well. Read on.


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!