Understand Abstract Class and Abstract Method using example


About Abstract Class

Abstract class in csharp is defined using "abstract" keyword
An abstract class may contain abstract methods and accessors (properties).
Non abstract methods or properties should provide actual implementation in an abstract class.

Abstract classes that cannot be instantiated mean we cannot able to create an object of an abstract class but abstract class can be implemented to child classes like a common base class. An abstract class can be partially implemented or not at all implemented.


abstract class absCalculator
...code starts here


Abstract Method

In an abstract class a method which has a keyword "abstract" and doesn't provide any implementation is called abstract method.The implementation logic of abstract methods is provided by the child classes or derived classes.Child classes use keyword "override" with same method name (as abstract method name) to provide further implementation of abstract methods.

Non Abstract Method

In an abstract class a method which doesn't have a keyword "abstract" and  provide any implementation is called non abstract method.

Why Abstract Class

Abstract class enforces derived classes to provide all implementation logic for abstract methods or properties.

Use of an Abstract Class

Use abstract class when you want to create a common base class for a family of types and with some implementation
Subclass only a base class in a hierarchy to which the class logically belongs.

Below a simple example in c# which illustrates abstract class with abstract methods and non abstract methods.

First step lets create an abstract class "absCalculate" having abstract methods and non abstract methods.

abstract class absCalculate

            //A Non abstract method
            public int Addition(int Num1, int Num2)
                return Num1 + Num2;

            //An abstract method, to be overridden in derived class
            public abstract int Multiplication(int Num1, int Num2);

Now next step lets create a class "clsCalculate" and implement it with abstract class "absCalculate".

 class clsCalculate:absCalculate

And now lets build this application.

When we build this application we got the following error because we have not provided implementation of an abstract method as you can see it in above snapshot.

Now lets give implementation for an abstract method using override keyword implementing the abstract method as you can see in our below code.


class clsCalculate:absCalculate

          //using override keyword implementing the abstract method
          public override int Multiplication(int Num1, int Num2)
              return Num1 * Num2;


Finally lets create an object of class "clsCalculate" in our main program of an console application and see the output.


class Program

          static void Main(string[] args)

              Console.WriteLine("Please Enter First Number");
              int num1 = Convert.ToInt16(Console.ReadLine());

              Console.WriteLine("Please Enter Second Number");
              int num2 = Convert.ToInt16(Console.ReadLine());

              absCalculate objabCal = new clsCalculate();
              int add = objabCal.Addition(num1, num2);
              int multiplied = objabCal.Multiplication(num1, num2);
              Console.WriteLine("Added Number is : 0}, Multiplied Number is : 1}", add, multiplied);



Hey friends if you have any doubt or query kindly let me know through your comments.





More C# Articles To Read Next:


OOPS Principle - Abstraction in C# with an example and explanation


Views - 153070 |Category - C#


What are Delegates in C# - Step By Step Creating and Using the Delegate


Views - 18357 |Category - C#


Difference Between For Loop And For Each Loop in Csharp


Views - 22274 |Category - C#

Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory




Posted by Anusha on 2015-06-03

very easy to undertsand with simple examples.. Oops concepts are explained clearly . This is what every developers need to know . Thanks for the post

Posted by Vinayagamurugan on 2014-11-25

This is very easy to understand c# concepts with examples comparing to other sites.. I read OOPL concepts using c# can be learned clearly with the help of Onlinebuff.com. Thanks a lot.

Posted by Yogesh Chandra Upreti on 2014-08-25

So actually when we have to use abstract class in a live project give some examples.

Posted by Ferose on 2014-06-19

thanks for the post Please clear the following doubt If i have two abstract methods in the abstract class and i had derived the abstract class. I wold like to know whether two abstract method definition part has to be included in the derived class or any one abstract method can be defined

Posted by Saurabh Bansal on 2014-06-10

Thats gr8

Posted by Waqar on 2014-05-06

Good explanation

Posted by Waqar on 2014-05-06

Above is a good Explanation Can you teach me what is aggregation by value /by refrence/by composition

Posted by Badharinadh on 2014-04-15

HI gurunatha, absCalculate objabCal = new clsCalculate(); why we are creating obj like that in abstract class.pls send description to my email

Posted by Sharika on 2014-03-25

Thank you for this code.Can you explain the exact use of abstract class..

Posted by Raja on 2013-10-09

nice to understand in this program.

Posted by Ankit on 2013-09-16

what's the mean of composition in C#?