The term abstraction in programming refers to the concealment of unnecessary, albeit highly important information, and making visible to the user only certain key or requisite information. This is made possible through the use of abstract classes.
Abstract class in C# is essentially a class which does not permit the creation of objects. ‘Abstract’ here is a keyword or a modifier that causes that particular class or method to be abstract or restricted. An abstract class can only be accessed through inheritance from a different class.
Properties and Components of Abstract class in C#
There are certain factors which need to be there for an abstract class to be initiated or accessed. They have been listed as follows.
- An abstract class is used to create a base class that can be commonly accessed by several derived classes. To use or implement an abstract class, programmers are first required to create a derived class.
- The abstract class thus has to be inherited by sub classes, which may even override the function.
- Every abstract class requires at least one abstract method.
Example of an abstract class in C#
Since the mechanism is complicated and understandable to only those familiar with the programming language, it is better to start with a very simple example that will help explain the concept.
Suppose there exists a class ‘Tree’, with an existing designed method known as ‘type’. Nothing else is defined and no other particular information is provided – one does not know the kind of tree, or even the types that are possible.
If a specific example of a tree is available, then a separate class is created through the inheritance of the base class, Tree. The idea is that the new class, say Pine or Oak, cannot actually modify the construct or definition of the base class, that is, Tree. It has to create its own class, derived from class Tree, and define another method inherited from the base.
Advantages of abstract class in C#
- While sub classes can choose to either implement or override an abstract class, certain modifications can be made which necessitate that a certain function must absolutely be implemented.
- There is thus a great degree of flexibility when designing an abstract class, which respect to which methods must be imperatively implemented.
- The programmer can choose how many abstract methods they want in an abstract class. It is also possible to write the code without including abstract methods.
- An abstract class has to be defined only once and can be accessed or derived from as many times as necessary henceforth.
- An abstract class is very useful when there are a set of classes which have a degree of commonality but differ in their implementation of the abstract class.
Disadvantages of abstract class in C#
- An abstract class can be added to the class hierarchy only if there are no abstract classes already existing.
- Objects of any abstract class can only be referenced, never created.
- Despite the fact that abstract classes cannot be instantiated, it does not allow the use of static variables or methods.
Table of Contents