Click here to Skip to main content
15,071,262 members
Articles / Programming Languages / C#
Posted 17 Feb 2004


202 bookmarked

All about abstract classes.

Rate me:
Please Sign up or sign in to vote.
4.59/5 (267 votes)
26 Feb 20043 min read
This article provides a quick introduction to abstract classes in .NET.


Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.

An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

An example of an abstract class declaration is:

abstract class absClass

An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.

An example of an abstract method:

abstract class absClass
  public abstract void abstractMethod();

Also, note that an abstract class does not mean that it should contain abstract members. Even we can have an abstract class only with non abstract members. For example:

abstract class absClass
    public void NonAbstractMethod()
        Console.WriteLine("NonAbstract Method");

A sample program that explains abstract classes:

using System;

namespace abstractSample
      //Creating an Abstract Class
      abstract class absClass
            //A Non abstract method
            public int AddTwoNumbers(int Num1, int Num2)
                return Num1 + Num2;

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

      //A Child Class of absClass
      class absDerived:absClass
            static void Main(string[] args)
               //You can create an
               //instance of the derived class

               absDerived calculate = new absDerived();
               int added = calculate.AddTwoNumbers(10,20);
               int multiplied = calculate.MultiplyTwoNumbers(10,20);
               Console.WriteLine("Added : {0}, 
                       Multiplied : {1}", added, multiplied);

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

In the above sample, you can see that the abstract class absClass contains two methods AddTwoNumbers and MultiplyTwoNumbers. AddTwoNumbers is a non-abstract method which contains implementation and MultiplyTwoNumbers is an abstract method that does not contain implementation.

The class absDerived is derived from absClass and the MultiplyTwoNumbers is implemented on absDerived. Within the Main, an instance (calculate) of the absDerived is created, and calls AddTwoNumbers and MultiplyTwoNumbers. You can derive an abstract class from another abstract class. In that case, in the child class it is optional to make the implementation of the abstract methods of the parent class.


//Abstract Class1
abstract class absClass1
    public abstract int AddTwoNumbers(int Num1, int Num2);
    public abstract int MultiplyTwoNumbers(int Num1, int Num2);

//Abstract Class2
abstract class absClass2:absClass1
    //Implementing AddTwoNumbers
    public override int AddTwoNumbers(int Num1, int Num2)
        return Num1+Num2;

//Derived class from absClass2
class absDerived:absClass2
    //Implementing MultiplyTwoNumbers
    public override int MultiplyTwoNumbers(int Num1, int Num2)
        return Num1*Num2;

In the above example, absClass1 contains two abstract methods AddTwoNumbers and MultiplyTwoNumbers. The AddTwoNumbers is implemented in the derived class absClass2. The class absDerived is derived from absClass2 and the MultiplyTwoNumbers is implemented there.

Abstract properties

Following is an example of implementing abstract properties in a class.

//Abstract Class with abstract properties
abstract class absClass
    protected int myNumber;
    public abstract int numbers

class absDerived:absClass
    //Implementing abstract properties
    public override int numbers
            return myNumber;
            myNumber = value;

In the above example, there is a protected member declared in the abstract class. The get/set properties for the member variable myNumber is defined in the derived class absDerived.

Important rules applied to abstract classes

An abstract class cannot be a sealed class. I.e. the following declaration is incorrect.

abstract sealed class absClass

Declaration of abstract methods are only allowed in abstract classes.

An abstract method cannot be private.

private abstract int MultiplyTwoNumbers();

The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.

public abstract virtual int MultiplyTwoNumbers();

An abstract member cannot be static.

publpublic abstract static int MultiplyTwoNumbers();

Abstract class vs. Interface

An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

An example of interface:

interface iSampleInterface
  //All methods are automaticall abstract
  int AddNumbers(int Num1, int Num2);
  int MultiplyNumbers(int Num1, int Num2);

Defining an abstract class with abstract members has the same effect to defining an interface.

The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.

A class can inherit one or more interfaces, but only one abstract class.

Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

The selection of interface or abstract class depends on the need and design of your project. You can make an abstract class, interface or combination of both depending on your needs.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Program Manager
India India
No Biography provided

Comments and Discussions

QuestionAbstract classes can add more functionality without destroying the child classes that were using the old version Pin
verma.vivek3-Oct-18 8:04
Memberverma.vivek3-Oct-18 8:04 
AnswerRe: Abstract classes can add more functionality without destroying the child classes that were using the old version Pin
OriginalGriff3-Oct-18 8:06
mveOriginalGriff3-Oct-18 8:06 
SuggestionJust some real time use Pin
Devd2229-Jul-18 18:20
MemberDevd2229-Jul-18 18:20 
QuestionAbstract class constructor Pin
DalachhaSolanki15-Jun-18 16:11
MemberDalachhaSolanki15-Jun-18 16:11 
QuestionInformative Article Pin
Shail Mishra17-Aug-17 20:49
MemberShail Mishra17-Aug-17 20:49 
PraiseVery nice article Pin
Member 108593553-Apr-17 2:27
MemberMember 108593553-Apr-17 2:27 
GeneralNice Pin
Member 1266534312-Mar-17 19:23
MemberMember 1266534312-Mar-17 19:23 
QuestionJayababu Pin
Dattatray Thorat3-Mar-16 19:51
professionalDattatray Thorat3-Mar-16 19:51 
QuestionThank You Pin
Ajit Kumar26825-Aug-15 2:08
professionalAjit Kumar26825-Aug-15 2:08 
SuggestionPlease add few points Pin
santosh panchal28-Jul-15 22:31
Membersantosh panchal28-Jul-15 22:31 
GeneralOnly Thanks Pin
himanshu singh26-Jun-15 0:58
Memberhimanshu singh26-Jun-15 0:58 
GeneralRe: Only Thanks Pin
Raja Naveed Aslam6-Mar-16 18:19
professionalRaja Naveed Aslam6-Mar-16 18:19 
GeneralThanks Pin
Ali Bagherzadeh5-Apr-15 22:32
MemberAli Bagherzadeh5-Apr-15 22:32 
GeneralAwesome !! Pin
jakss5-Apr-15 19:05
Memberjakss5-Apr-15 19:05 
Questionthe differences between " interfaces " and " abstract Class " Pin
Member 1152352715-Mar-15 12:32
MemberMember 1152352715-Mar-15 12:32 
AnswerRe: the differences between " interfaces " and " abstract Class " Pin
Member 867259423-Apr-15 5:03
MemberMember 867259423-Apr-15 5:03 
GeneralRe: the differences between " interfaces " and " abstract Class " Pin
cbcvb6-Sep-15 7:57
Membercbcvb6-Sep-15 7:57 
GeneralRe: the differences between " interfaces " and " abstract Class " Pin
Member 959142916-Jun-20 22:47
MemberMember 959142916-Jun-20 22:47 
QuestionCheck the below link for abstract class Pin
yuvarajarane18-Jan-15 7:58
Memberyuvarajarane18-Jan-15 7:58 
GeneralThanks this was really helpful Pin
Tharinda_tpw20-Oct-14 1:02
MemberTharinda_tpw20-Oct-14 1:02 
GeneralMy vote of 4 Pin
Sooraj_Singh10-Sep-14 20:44
professionalSooraj_Singh10-Sep-14 20:44 
Questionmy vote 3 Pin
Abhishek Jaiswall10-Sep-14 8:21
MemberAbhishek Jaiswall10-Sep-14 8:21 
Questionthnx Pin
Mahmoud_Mohamed29-Aug-14 16:38
MemberMahmoud_Mohamed29-Aug-14 16:38 
QuestionAbstract classes and interfaces Pin
Bernard Czaiński20-Aug-14 7:07
MemberBernard Czaiński20-Aug-14 7:07 
QuestionHere is another practical example: Pin
dietmar paul schoder2-Aug-14 10:26
professionaldietmar paul schoder2-Aug-14 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.