What is abstract Class in C#

In this article we will explain you What is abstract Class in C#, its key area and benefits. We will discuss all with the help of example below. We can create an abstract class by decorating it with abstract keyword. Data abstraction is the process of hiding certain details and showing only essential information to the end user. This abstraction can only be achieved with either abstract classes or by using interfaces.

Features of abstract class.

  • Abstract class always act a base class and is inherited by other classes.
  • It can contain constructors and destructors.
  • One cannot create an object of abstract class.
  • Abstract class can never be static.
  • Multiple inheritances is not supported in abstract class.
  • Abstract class is used as a design concept in programming and provides a base upon which other classes are built.
  • Abstract class can contain both abstract and non-abstract methods in it.
  • All abstract method of abstract class must be implemented in child class.
  • In C# we have “Abstract” keyword whereas in VB.NET abstract classes are created using “MustInherit” keyword.
  • Abstract method does not have a body. Its body is provided or written in the derived class. 

Code sample of abstract class

abstract class  BankBaseClass    // this is our abstract class
{
  public abstract void insertData();   // abstract method
  public abstract void updateData();    // abstract method
  public abstract void deleteData();    // abstract method
  public abstract void showData();     // abstract method

  public void ValidateUser()    // Non abstract method 
  {
    Console.WriteLine("User validated successfully");
  }
}

// *******  in following class we will inherit abstract class.****** 
public class Bank : BankBaseClass
{
 
  // 'override' keyword is used to use base class methods as shown below
    public override void insertData()
    {
        Console.WriteLine(“Insert data method is called”);
    }
    public override void updateData()
    {
        Console.WriteLine(“Update data method is called”);
    }
    public override void deleteData()
    {
        Console.WriteLine(“Delete data method is called”);
    }
    public override void showData()
    {
        Console.WriteLine(“Show data method is called”);
    }
}

In above example we have taken an abstract class named “BankBaseClass“. It contains abstract method and non-abstract method too. All abstract methods of abstract class need to be implemented in derived class named “Bank” and it must require implementing. Next ‘override’ keyword is used to use abstract class methods in the derived class as shown below:

   public override void insertData()
    {
        Console.WriteLine(“Insert data method is called”);
    }

Difference between Interface and Abstract class

  • Abstract classes can have both abstract and non-abstract method whereas in Interface all methods are abstract by default.
  • Multiple Inheritance in not feasible with abstract class but feasible with interfaces.
  • In abstract classes “abstract” keyword is used and for interfaces we use “interface” keyword.
  • We can have constructor in “abstract” class but not in interfaces.
  • Abstract class can have static data members, but interfaces cannot.
  • Performance of Abstract class is fast as compared to Interfaces.
  • Abstract class can be implemented fully or partially but Interface are implemented fully.

What is the use of abstract class?

In nutshell we can say that an abstract class is used to provide a base for subclasses. It forces client class to use the same structure. For example, we have discussed one example above which have 4 abstract methods and client class must have to implement all these 4 methods, it will give same pattern to our multiple client classes, and this is for what abstract class is known as.

What is the difference between virtual and abstract methods?

Virtual: methods in abstract class can have its body and it provide the option to client class for implementation too. It depends upon user either to implement virtual method or not, but it does not force for implementation like abstract methods. Additionally, virtual methods can be created in abstract and non-abstract class.

Abstract: methods of abstract class do not have body and are forced to implement in the client class.

public abstract class A
{
    public abstract void Method1();

    public virtual void Method2()
    {
        // Default implementation which can be overridden by subclasses.
    }
}

public class B:A
{
    public override void Method1()
    {
        // You must have to override this method because it is abstract in above class.
    }
    public override void Method2()
    {
        // It is Virtual in base class, we can override this method
    }
}

Can one Abstract class be inherited in another abstract class?

Yes, we can inherit one abstract class in another abstract class.

Can Abstract or Virtual methods be created as sealed?

Abstract and Virtual method cannot be crated as sealed. Abstract class always created as base class and is inherited by other classes, if it will be SALED then its actual behavior of inheritance will become useless. That is why it is not supported and if you’ll forcefully add ‘Sealed’ in abstract methods then it will give compile time error as show in below image.

What is abstract Class in C#

Can abstract class or abstract method be static?

No, an abstract method cannot be static. It will give compile time error. Additionally, default use of abstract method is that it needs to be override in derived class and it will not be feasible with static. Hence, it is not supportive in abstract class.

In this article we have explained you What is abstract Class, its characteristics and benefits. We hope this has cleared your doubt on Abstract class. For more information you can visit HERE.

For more information you can visit our C# Section for more such articles.

Leave a Reply

Your email address will not be published. Required fields are marked *