A Deep Dive into C# Abstract Class

22 Aug 2022
Advanced
174K Views

An abstract class is a special type of class that cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.

An abstract class can be used as a base class and all other derived classes should be implemented the abstract class definitions. All the abstract methods should be implemented in all non-abstract classes using the keyword called an override. and after overriding, the abstract method is in the non-abstract class. we can derive this class in another class and again we can override the same abstract method with it ultimately.

The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.

For example, a class library may define an abstract class that is used as a parameter to many of its functions and require programmers to use that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class.

abstract class ShapesClass
{
 abstract public int Area();
}
class Square : ShapesClass
{
 int side = 0;

 public Square(int n)
 {
 side = n;
 }
 // Override Area method 
 public override int Area()
 {
 return side * side;
 }
}

class Rectangle : ShapesClass
{
 int length = 0, width=0;

 public Rectangle (int length, int width)
 {
 this.length = length;
 this.width = width;
 }
 // Override Area method 
 public override int Area()
 {
 return length * width;
 }
}

Abstract methods

A method that is declared as an abstract method, has no “body” and it can be declared inside the abstract class only which is the thumb rule.

An abstract method should be implemented in all non-abstract classes using the keyword called "override". And after overriding the methods, the abstract method is in the non-abstract class context. Below is a simple example to create the abstract method.

public abstract void getData(); // where the method called 'getData()' is a abstract method

Abstract Class

In C#, an abstract class is a class that is declared using the keyword "abstract". An abstract class can have a set of abstract and non-abstract methods. We should remember that the abstract class cannot be instantiated. Abstract class implementation must be provided by derived classes. To work with the abstract classes, the respective derived class is forced to provide the implementation of all the abstract methods created within the abstract class.

Let's see a simple example of an abstract class in C# that has one abstract method declared which is "drive()". And its implementation can be provided by derived classes as required.

using System; 
public abstract class Car 
{ 
 public abstract void drive(); 
} 

Features of Abstract Class

  1. An abstract class cannot be instantiated.

  2. An abstract class contains abstract members as well as non-abstract members.

  3. An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

  4. A non-abstract class that is derived from an abstract class must include actual implementations of all the abstract members of the parent abstract class.

  5. An abstract class can be inherited from a class and one or more interfaces.

  6. An Abstract class can have access modifiers like private, protected, and internal with class members. But abstract members cannot have a private access modifier.

  7. An Abstract class can have instance variables (like constants and fields).

  8. An abstract class can have constructors and destructors.

  9. An abstract method is implicitly a virtual method.

  10. Abstract properties behave like abstract methods.

  11. An abstract class cannot be inherited by structures.

  12. An abstract class cannot support multiple inheritances.

Common design guidelines for Abstract Class

  1. Don't define public constructors within an abstract class. Since an abstract class cannot be instantiated and constructors with public access modifiers provide visibility to the classes which can be instantiated.

  2. Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly which contains that class.

When to use

  1. Need to create multiple versions of your component since versioning is not a problem with abstract classes. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.

  2. Need to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

What do you think?

The concepts of abstraction with the methods and classes are an extension to the inheritance were the inheritance we have been discussing that with the help of a parent class we can provide property to the child class that can be consumed by the child classes which gives us the code re-usability while sticking to the security accessibility.

I hope, now you have got everything about the abstract class. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Learn to Crack Your Technical Interview

Accept cookies & close this