Abstract Class and Abstract Methods in C#
In this article, I am going to discuss Abstract class and Abstract methods in C# with Examples. Please read our previous article, where we discussed Inheritance in C#. At the end of this article, you will understand what are abstract class and abstract methods, why do we need abstract class and abstract methods, and how to implement this in C#.
What is an Abstract Class in C#?
A class that is declared by using the keyword abstract is called an abstract class. An abstract class is a partially implemented class used for implementing some of the operations of an object which are common for all next-level subclasses and the remaining abstract methods to be implemented by the next-level subclasses. So it contains both abstract methods and concrete methods including variables, properties, and indexers.
It is always created as a superclass next to the interface in the object inheritance hierarchy for implementing common operations from an interface. An abstract class may or may not have abstract methods. But if a class contains an abstract method then it must be declared as abstract.
An abstract class cannot be instantiated directly. It’s compulsory to create/derive a subclass from the abstract class in order to provide the functionality to its abstract functions.
What is the abstract method in C#?
A method that does not have a body is called an abstract method. It is declared with the modifier abstract. It contains only a declaration/signature and does not contain the implementation/body/definition of the method. An abstract function should be terminated with a semicolon. Overriding an abstract function is compulsory.
Why should the method have an abstract keyword if it does not have a body in C#?
In a class, we are allowed only to define a class with the body. Since we are changing its default behavior (which means removing its body) it must have the abstract keyword in its prototype.
Points to Remember while working with an abstract class in C#
An abstract class can contain both abstract methods and non-abstract (concrete) methods.
It can contain both static and instance variables.
The abstract class cannot be instantiated but its reference can be created.
If any class contains abstract methods then it must be declared by using the keyword abstract.
An abstract class can contain sealed methods but an abstract method or class cannot be declared as sealed.
A subclass of an abstract class can only be instantiated if it implements all of the abstract methods of its superclass. Such classes are called concrete classes to differentiate them from abstract classes.
When should a class be declared as abstract?
A class should be declared as abstract
If the class has any abstract methods
If it does not provide implementation to any of the abstract methods it inherited
If it does not provide implementation to any of the methods of an interface
When to use the abstract method in C#?
Abstract methods are usually declared where two or more subclasses are expected to fulfill a similar role in a different manner. Often the subclasses are required to fulfill an interface, so the abstract superclass might provide several of the interface methods, but leave the subclasses to implement their own variations of the abstract methods.
Child class of abstract class:
Implements all the abstract methods of the parent
Then only we can consume the non-abstract methods of the parent.
Rules Of Abstract Method and Abstract Class in C#:
Rule1: If a method does not have the body, then it should be declared as abstract using the abstract modifier else it leads to a compile-time error: “must declare a body because it is not marked abstract, extern, or partial”