Click here to Skip to main content
11,412,516 members (72,457 online)
Click here to Skip to main content

All about abstract classes.

, 26 Feb 2004
Rate this:
Please Sign up or sign in to vote.
This article provides a quick introduction to abstract classes in .NET.

Introduction

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
      {
            [STAThread]
            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
            //MultiplyTwoNumbers
            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.

Example

//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
    {
        get;
        set;
    }
}

class absDerived:absClass
{
    //Implementing abstract properties
    public override int numbers
    {
        get
        {
            return myNumber;
        }
        set
        {
            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.

//Incorrect
abstract sealed class absClass
{
}

Declaration of abstract methods are only allowed in abstract classes.

An abstract method cannot be private.

//Incorrect
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.

//Incorrect
public abstract virtual int MultiplyTwoNumbers();

An abstract member cannot be static.

//Incorrect
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.

License

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

Share

About the Author

Jayababu
Program Manager
India India
No Biography provided

Comments and Discussions

 
QuestionCheck the below link for abstract class Pin
yuvarajarane at 18-Jan-15 8:58
memberyuvarajarane18-Jan-15 8:58 
GeneralThanks this was really helpful Pin
Tharinda_tpw at 20-Oct-14 2:02
memberTharinda_tpw20-Oct-14 2:02 
GeneralMy vote of 4 Pin
Sooraj_Singh at 10-Sep-14 21:44
professionalSooraj_Singh10-Sep-14 21:44 
Questionmy vote 3 Pin
Abhishek Jaiswall at 10-Sep-14 9:21
memberAbhishek Jaiswall10-Sep-14 9:21 
Questionthnx Pin
Eng-Mondy at 29-Aug-14 17:38
memberEng-Mondy29-Aug-14 17:38 
QuestionAbstract classes and interfaces Pin
Bernard Czaiński at 20-Aug-14 8:07
memberBernard Czaiński20-Aug-14 8:07 
QuestionHere is another practical example: Pin
Dietmar Schoder at 2-Aug-14 11:26
professionalDietmar Schoder2-Aug-14 11:26 
QuestionHi jay Pin
avanigaddagopal at 28-Jul-14 5:21
memberavanigaddagopal28-Jul-14 5:21 
QuestionAbstract class explanation Pin
Member 10185335 at 28-Jul-14 1:45
memberMember 1018533528-Jul-14 1:45 
GeneralGood One..! Pin
Gurunatha Dogi at 12-Jun-14 2:33
memberGurunatha Dogi12-Jun-14 2:33 
GeneralMy vote 5 Pin
sansuna at 29-May-14 22:00
membersansuna29-May-14 22:00 
GeneralMy vote of 2 Pin
Member 10706267 at 2-Apr-14 2:15
memberMember 107062672-Apr-14 2:15 
AnswerThanks Pin
Harikrishnanvr at 6-Mar-14 8:52
memberHarikrishnanvr6-Mar-14 8:52 
QuestionThanks a lot for posting example in simple way. Pin
Member 10531050 at 4-Mar-14 4:19
memberMember 105310504-Mar-14 4:19 
GeneralVery good explanation! Pin
karthik_bv2 at 7-Feb-14 20:05
memberkarthik_bv27-Feb-14 20:05 
AnswerMessage Automatically Removed Pin
at 17-Jan-14 23:48
professionalRavi.Kumar02117-Jan-14 23:48 
QuestionGood Pin
mario_mamo at 27-Nov-13 1:17
membermario_mamo27-Nov-13 1:17 
QuestionGood Pin
rllaksh at 13-Nov-13 0:30
memberrllaksh13-Nov-13 0:30 
GeneralGood Pin
Rajesh Toleti at 18-Sep-13 4:36
memberRajesh Toleti18-Sep-13 4:36 
GeneralMy vote of 5 Pin
Member 7826530 at 3-Sep-13 4:13
memberMember 78265303-Sep-13 4:13 
GeneralMy vote of 5 Pin
AlexOragz at 25-Aug-13 5:17
memberAlexOragz25-Aug-13 5:17 
Questionplease clear my doubt....!! Pin
Rabbil at 7-Jul-13 22:19
memberRabbil7-Jul-13 22:19 
GeneralMy vote of 3 Pin
shamimbd0 at 13-Jun-13 9:11
membershamimbd013-Jun-13 9:11 
GeneralMy vote of 4 Pin
Member 10056887 at 16-May-13 3:35
professionalMember 1005688716-May-13 3:35 
GeneralRe: My vote of 4 Pin
Member 10584056 at 9-Feb-14 20:47
memberMember 105840569-Feb-14 20:47 
GeneralRe: My vote of 4 Pin
Member 11123997 at 9-Mar-15 8:03
memberMember 111239979-Mar-15 8:03 
QuestionC#.net Pin
tarun vats at 21-Mar-13 21:36
membertarun vats21-Mar-13 21:36 
GeneralMy vote of 5 Pin
dpalash at 26-Feb-13 10:10
memberdpalash26-Feb-13 10:10 
GeneralMy vote of 2 Pin
pradeepkurmi at 31-Jan-13 3:15
memberpradeepkurmi31-Jan-13 3:15 
GeneralMy vote of 5 Pin
Vijaychauhan0806 at 27-Jan-13 23:08
memberVijaychauhan080627-Jan-13 23:08 
GeneralMy vote of 5 Pin
Member-3948663 at 1-Jan-13 19:23
memberMember-39486631-Jan-13 19:23 
GeneralMy vote of 4 Pin
tariq_619 at 9-Dec-12 6:12
membertariq_6199-Dec-12 6:12 
Questionthanks Pin
Md. YaSiR aRaFaT at 18-Nov-12 20:53
memberMd. YaSiR aRaFaT18-Nov-12 20:53 
GeneralMy vote of 4 Pin
Md. YaSiR aRaFaT at 18-Nov-12 20:51
memberMd. YaSiR aRaFaT18-Nov-12 20:51 
Questiongreat article.... Pin
Didier Monge at 3-Nov-12 12:42
memberDidier Monge3-Nov-12 12:42 
QuestionAbout Static methods in Abstract class Pin
tech expert at 5-Oct-12 1:33
membertech expert5-Oct-12 1:33 
GeneralMy vote of 4 Pin
rahulshek at 28-Sep-12 2:37
memberrahulshek28-Sep-12 2:37 
easy to understand
QuestionAbstract class without abstract method ??? Pin
omvikram at 24-Sep-12 8:12
memberomvikram24-Sep-12 8:12 
GeneralMy vote of 4 Pin
veenavipeesh at 21-Sep-12 7:24
memberveenavipeesh21-Sep-12 7:24 
Questionabout usage of abstract methods Pin
minehelp at 27-Jun-12 21:26
memberminehelp27-Jun-12 21:26 
Suggestionabout clarification Pin
Member 8954696 at 16-Jun-12 4:08
memberMember 895469616-Jun-12 4:08 
GeneralMy vote of 5 Pin
pandianRB at 13-Jun-12 1:39
memberpandianRB13-Jun-12 1:39 
GeneralMy vote of 1 Pin
pandianRB at 13-Jun-12 1:38
memberpandianRB13-Jun-12 1:38 
QuestionAbstract and interface Pin
Rubel Khan at 2-Jun-12 10:24
memberRubel Khan2-Jun-12 10:24 
AnswerRe: Abstract and interface Pin
Aryan_MSD at 14-Jun-12 22:13
memberAryan_MSD14-Jun-12 22:13 
GeneralVery very nice article Pin
Jatinder RahejaTCS at 27-May-12 2:55
memberJatinder RahejaTCS27-May-12 2:55 
QuestionImplementation of Abstract Method in Derived abstract class Pin
Asjad Idrisi at 24-May-12 20:32
memberAsjad Idrisi24-May-12 20:32 
AnswerRe: Implementation of Abstract Method in Derived abstract class Pin
Aryan_MSD at 14-Jun-12 22:24
memberAryan_MSD14-Jun-12 22:24 
GeneralMy vote of 5 Pin
GuyThiebaut at 22-May-12 21:15
memberGuyThiebaut22-May-12 21:15 
GeneralMy vote of 5 Pin
H John at 21-May-12 21:24
memberH John21-May-12 21:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 27 Feb 2004
Article Copyright 2004 by Jayababu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid