Wednesday, January 29, 2020 05:13

Abstract classes and methods

As I was explaining in the previous lesson, one way of achieving abstraction is trough the means of abstract classes and methods. The abstract keyword can be used for both classes and methods. An abstract class is a class that provides a partial implementation. This means that they are incomplete classes, they cannot be used on their own. We cannot instantiate abstract classes, by using the new operator. Abstract classes must be inherited in child classes, that complete their partial implementation. For this reason, abstract classes are sometimes called abstract base classes (when not all of their methods are also abstract), or pure abstract base classes (when all methods declared inside them are also declared as abstract). Abstract classes can contain both abstract and normal methods, and they can also contain no abstract methods at all.

Abstract methods cannot be declared outside of abstract classes. If you need a method to be declared as abstract, you also must declare the class that contains it as abstract. Abstract methods also cannot have a body. The body is supplied in the class that inherits from the abstract class.

Since the sealed keyword means that the class cannot be inherited, and since abstract classes must be inherited in order to complete their partial implementation, you are not allowed to declare abstract classes as sealed.

Here is an example of a simple abstract class:

In order to use the above abstract class, we need to inherit from it, and provide an implementation for the abstract method:

We declared another class, Cat, which inherits the abstract class Animal, thus inheriting its methods. Here, you notice that we must declare an override version of the Eat() method, which was declared abstract, and had no body when we declared it. If we wouldn’t override the abstract method, effectively offering it a body, a concrete implementation, we would get a compiler error: Error CS0534 ‘Cat’ does not implement inherited abstract member ‘Animal.Eat()’. After we declare a class that inherits and completes the implementation of the abstract class, we can instantiate it and call the methods inside it. In other words, we were able to specify that the class Animal is incomplete, not ready to be instantiated, and only its successors can have instances.

As a conclusion, abstract classes are something in the middle between classes and interfaces. They can define ordinary methods and abstract methods. Ordinary methods have an implementation, whereas abstract methods are empty (without an implementation) and remain to be implemented later by the derived classes. A pure abstract class is very similar to an interface (described in next lesson), the only difference being the fact that a class can implement multiple interfaces, but can only inherit one class (even if abstract). Initially, interfaces were not necessary in the presence of “multiple inheritance”. They had to be conceived as a means to supersede it in specifying the numerous roles of an object.

Comments

comments

Tags: , , , , ,

Leave a Reply



Do NOT follow this link or you will be banned from the site!