Abstract Classes and Methods
In this lesson, you'll get to know about abstract classes and methods.
We'll cover the following
Abstract Methods
An abstract method is declared using the
abstract
keyword and does not have a body/ implementation.
There are certain rules we should follow when working with abstract methods. Let’s have a look at these rules.
Rules to be Followed
-
In contrast to a non-abstract/normal C# method, an abstract method does not have a body/definition, i.e., it only has a declaration or method signature.
-
An abstract method can be declared inside an abstract class or an interface only, more on these later.
-
To contain any abstract method in its implementation, a class has to be declared as an abstract class. Non-abstract classes cannot have abstract methods.
-
An abstract method cannot be declared with the
private
orsealed
modifier as it has to be implemented in some other class. -
Abstract methods are implicitly
virtual
so we cannot use the keywordvirtual
in their declaration.
Just like abstract methods, an abstract property declaration does not provide an implementation of the property accessors, i.e.,
get
andset
blocks. It declares that the class supports properties but leaves the accessor implementation to derived classes.
Declaration
Let’s move on to the syntax part. Syntactically, the generalized declaration of an abstract method is as follows:
Get hands-on with 1400+ tech skills courses.