Data Haunting

Easy Solutions Destination

By

C# Abstract class with example

Abstract class in C# acts as a base class. Abstract Class can not be instantiated and can only be derived by its child classes. The need of abstract class is to provide some basic functionality which is used by it’s child classes so they can override and share. Let’s take a look at  member of  abstract class and abstract method.

  • Abstract Class Features with example

  1. Abstract class in c# is defined by “Abstract” keyword.
  2. Abstract class can not be instantiated  and can only be derived by it’s child classes and interface.
  3. Abstract class can have abstract and non-abstract method as well as abstract and non-abstract properties.
  4. A non-abstract class or interface those are derived from Abstract class, they all have to implement it’s Parent abstract class’s all abstract method and property.

Here is the example of Abstract Class

  •  Abstract Method Features with example

  1. Abstract class have two type method abstract method and non-abstract method.
  2. Method which have “abstract” keyword and does not provide implementation is called abstract method .
  3. Implementation logic of abstract method is provided by its child classes with use of “override” keyword .
  4. In abstract class method which does not have keyword “abstract” and provide implementation is called non-abstract method.

Note – It is necessary that all abstract method must be implemented by it’s child classes.

Example of abstract method and non-abstract method :

In above example we have created abstract class “AbsCalculations” and two method among one is abstract method “MultiplyNumber” which contains abstract keyword and does not provide implementation logic. This method must be override in it’s derived or child class. While second method is non-abstract method “SumOfNumber” which does not contain abstract keyword and provide implementation logic.

Inherit Abstract class in derived class or child class

Now we created one class “clsCalculations” which is inherited or derived from our abstract class “AbsCalculations”. Here in this class “clsCalculations” we have override abstract method “MultiplyNumber” of abstract class “AbsCalculations” as well also provide logical implementation. It is necessary that derived class have to override all it’s parent abstract class’s abstract method otherwise it will throw error.

Create instance of Abstract class’s derived or child class

Now suppose by mistake if we create instance of abstract class it will give you error can not create instance of abstract class or interface as earlier we have specified. Let’s see example in which I have tried to call abstract class by creating it’s instance.

Now we will see proper way for creating abstract class’s child class or derived class instance and calling of abstract and non abstract method using console application.

As we have seen in example we can create instance of abstract class’s derived class “clsCalculations ” using two way. Using instance of “clsCalculations” we can call abstract method “MultiplyNumber” and non-abstract method “SumOfNumber”.

  • When to use Abstract Class

  1. When we need to provide common or basic functionality and behaviors  that multiple class can share and override
  2. when concept like multiple version  is arrived , we can use abstract class as we can add abstract method and property without breaking code so child class will update automatically its change .

What is your opinion ?

Here I have explained Abstract class and Abstract method with example plus how to use in our application. I hope it will be very useful to understand abstract keyword. If you have any query you can comment or you can mail me.

3 Responses to C# Abstract class with example

  1. Vinidimple says:

    Hi , I have created a dll, which has an abstract class also implements an interface and few derived classes which implements the abstract class. now i want to call this dll from an outside project, when i try to call the dll, it shows the abstract class only visible to me along the abstract methods. which is the best way to call the dll 1) create an abstract method which calls the derived classes 2) make a direct call to the derived class outside of dll?

    • Dilip Patel says:

      Hi Vinidimple ! my suggestion to you go with second solution that is (2) make a direct call to the derived class outside of dll because to create an abstract method which calls the derived classes, it’s not a good idea because concept of abstract method is to give implementation in it’s derive class and not to create an instance of derived class from that abstract method. You may face logic error if you 1) create an abstract method which calls the derived classes.

Leave a Reply