This is my first article to this booming educational hub C# Corner. This platform is amazing for learning and nurturing your skills with a huge collection of highly qualified professionals.
There is a large number of articles written for this topic. I am writing this for beginners.
Abstract Class
An Abstract class is an incomplete class with abstract members that cannot be instantiated and are either partially implemented or not at all implemented.
In other words this class does not have an implementation but only declarations of abstract methods so it does not make sense to make an instance of an incomplete class. It can have non-abstract methods with implementation.
An Abstract class can act as a base class in any project and the classes to be inherit from this base abstract class need to provide an implementation for the abstracts methods of the abstract base class.
An Abstract class can be created using the "abstract" keyword as in the following:
Public Abstract Class Employees
{
// provide Declaration of a abstract methods
Public abstract void Getdata();
// Non Abstract methods
Public void displayData()
{
//Code Please
}
}
When a class is inherited from a base abstract class it has two options.
Either the derived class must implement all the abstract methods or the derived class should be an abstract class.
Let us see what that means.
Taking an example of a customers class having three methods, two abstract and one non-abstract. The following abstract methods only have a declaration provided but for the non-abstract method Add() you need to provide an implementation.
public abstract class customers
{
public customers()
{
//can initialization code be provided.
}
public abstract void display();
public abstract void Print();
public void Add()
{
// Add Code here
}
}
When this abstract class is being inherited by a derived class.
Here the program class has been inherited from the customers class and we need to provide an implementation for the abstract methods display() and Print().
class Program : customers
{
public override void display()
{
Console.WriteLine("Customer Data displayed");
}
public override void Print()
{
Console.WriteLine("Customer Data Printed");
}
static void Main(string[] args)
{
// Program p = new Program();
customers c = new Program();
c.display();
Console.ReadLine();
}
}
If you don't want to provide an implementation for the abstract methods from the base abstract class then the Derived class should be abstract.
abstract class Program : customers
{
static void Main(string[] args)
{
// Program p = new Program();
// customers c = new Program();
// c.display();
Console.ReadLine();
}
}
Important Sticky
- An abstract class contains abstract members as well as non-abstract members and cannot be instantiated.
- 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.
- 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 and an abstract class cannot be inherited by structures.
- Public constructors should not be within an abstract class since an abstract class cannot be instantiated and constructors with public access modifiers provide visibility to the classes that can be instantiated.
Where and why abstract should be used.
This is very important to understand why and where abstract classes should be used.
The following is some of the reasons.
- Avoid Code duplication
An Abstract class can be used to move common functionality/code from interrelated non-abstract classes in a project into base abstract class. All the common functionality will be available to derived classes and they can provide various versions of imlementation depending on their needs.
- Code Maintainability
You can add properties or methods to an abstract class without breaking the code and all the inheriting classes are automatically updated with the change. This will make project code easily maintainable and enhancements will be easy for the developers.
- Need certain functionality must be implemented in the all derived classes.
If we have the scenario in the project where certain functionality must be implemented by derived classes then abstract will be one of the better options. That functionality can be made abstract in the base class.
By making the method abstract we are putting a restriction of providing an implementation in every derived class that has inherited the base abstract.