Abstract in CSharp
In this article we deal with the term abstract in CSharp and thus what we can do with it.
abstract is a modifier. The abstract modifier can be used for classes, functions, properties, indexers, and events. If a function or an interface is marked with the modifier abstract, the implementing class must also be declared abstract. If a class is marked with abstract, then it is not possible to create objects from it.
A subclass inheriting from an abstract class must completely implement all the abstract functions of the superclass, otherwise it must also be declared abstract. Unlike an interface, an abstract class can also include implemented functions and attributes that are not declared as static or final. So if you do not implement an implementation with an abstract class, it makes more sense to declare it as an interface.
Example for abstract in CSharp
abstract class Car{
int speed;
abstract void drive();
}
The class car includes the abstract function drive. Since this method is marked as abstract, it has no function body. The function head is terminated directly by a semicolon. If a function is abstract in a class, the class itself must also be abstract. If, however, the class is abstract, the functions of the class need not necessarily be abstract.
Why does the function () have no function body ?! This is because our class car is abstract in this case, and therefore no object can be created by it. We know, however, that it is a superclass. The function drive() must be present in the subclasses and a function body is required there.
public class BMW : Car
{
public void drive(){
Console.WriteLine("I drive a BMW!");
}
}
The function drive of our subclass Audi also needs to have a body:
public class Audi {
public void drive() {
Console.WriteLine("I drive an Audi!");
}
}
Furthermore, we must take into account that the access rights to the function in the subclass must be identical to those of the superclass. So if a function is generated in the superclass …
void function();
Then the subclass can’t have a public as an access modifier.
Abstract methods have the following features:
- An abstract method is implicitly a virtual method.
- Abstract method declarations are only permitted in abstract classes.
- Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature. For example:
public abstract void MyMethod();The implementation is provided by an overriding methodoverride, which is a member of a non-abstract class.
- It is an error to use the static or virtual modifiers in an abstract method declaration.
Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
- It is an error to use the
abstract
modifier on a static property.- An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
You can find more in the documentation.