What is Abstract Class in C#? | Definition, Properties and Examples

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.

  1. 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.
  2. The abstract class thus has to be inherited by sub classes, which may even override the function.
  3. 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#

  1. 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.
  2. There is thus a great degree of flexibility when designing an abstract class, which respect to which methods must be imperatively implemented.
  3. 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.
  4. An abstract class has to be defined only once and can be accessed or derived from as many times as necessary henceforth.
  5. 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.
RECOMMENDED
Difference Between Java and J2EE (With Table)

Disadvantages of abstract class in C#

  1. An abstract class can be added to the class hierarchy only if there are no abstract classes already existing.
  2. Objects of any abstract class can only be referenced, never created.
  3. Despite the fact that abstract classes cannot be instantiated, it does not allow the use of static variables or methods.

References

  1. https://books.google.co.in/books?hl=en&lr=&id=UbJa11wLcqYC&oi=fnd&pg=PA125&dq=Abstract+Class+in+C%23&ots=y9lmboLlHj&sig=kk5oYAJSHpmo-FyHphcAlkUh4cc&redir_esc=y#v=onepage&q=Abstract%20Class%20in%20C%23&f=false
  2. https://books.google.co.in/books?hl=en&lr=&id=-w9ftEtwPGwC&oi=fnd&pg=PP8&dq=Abstract+Class+in+C%23&ots=XXCx6-5nlm&sig=CYfs0myaTnm1anSz4QUGcZ65uS4&redir_esc=y#v=onepage&q=Abstract%20Class%20in%20C%23&f=false
x
Fortnite vs PubG video