TOC

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

Třídy:

Abstraktní třídy

Abstraktní třídy označené klíčovým slovem "abstrakt" v definici třídy se obvykle používají k definování základní třídy v hierarchii. Co je na nich zvláštní, je to, že nemůžete vytvořit jejich instanci - pokud se pokusíte, dostanete chybu kompilace. Místo toho je musíte podřídit, jak je uvedeno v kapitole o dědictví, a vytvořit instanci vaší podtřídy. Takže kdy potřebujete abstraktní třídu? Opravdu záleží na tom, co děláte.

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.

V tomto příkladu vytvoříme základní třídu pro čtyřnohá zvířata a pak vytvoříme třídu psů, která z ní dědí, jako je to zde:

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!