What is an Abstract Class?

We usually think of classes as being complete definitions.  However, there are situations where complete definitions of a class is not possible.  In OO, we can create a class that have incomplete definitions ( we can define what methods should be there in the class, but not the definition of the method).  These  incompletly defined classes are called as Abstract Class. These incompletely defined classes are also very useful like completely defined classes.   We cannot create an instance of an Abstract Class (ie intansiated),  but with inheritance, abstract class enforces the derived class to have an implemention of methods that are declared abstract in base class.

A method with only method header and with no method body is called as Abstract Method.   A method with a method header and a method body is called as Concrete Method.

In abstract Class you can specify the method with only method header like in the following example

public abstract class BankAccount
{
      private string name;

     public void PrintName()  // Concrete Method
    {
          Console.WriteLine(name);
    }
    public  abstract void printAccountType(); // Abstract Method

}

In the above class, note that the class is also declared abstract.  Whenever a class contains one or more abstract methods, then the class should be declared Abstract.  It is not necessary that an abstract class should only contain abstract methods.  It can also contain Concrete methods.

The methods which donot have a definintion (or method body) is defined using the keyword abstract as in the above example.

The derived class of abstract class should have a method implementation that are declared abstract in base class.  For exampe

public class SavingsAccount : BankAccount
{
         public  override void printAccountType()
         {
              Console.WriteLine(" This is a savings account ");
         }
}

Note : If the derived class does not provide concrete implementation of all the abstract methods that it  inherited from the abstract class,  the derived class will still be an abstract class. Only when the derived class provides concrete implementation of all the methods from the abstract class, can it be intantiated.

Advertisements

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

6 thoughts on “What is an Abstract Class?”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s