Abstract Class and Method with C# – A detail description

What is Abstract Class?

Abstract class is a class that has no direct instances, but its descendants may have direct instances.  An abstract class is a class that can contain abstract members, although it is not required. But any class that contains abstract member must be abstract. An abstract class can also contain non-abstract members.

An abstract method is an empty method – a method that has no implementation. Any class that implement this abstract class must implement the abstract method. We always use interface in C#, it is implicitly abstract.

Why abstract class?

The purpose of an abstract class is to act as a base class. It is not possible to instantiate an abstract class directly, nor can an abstract class be sealed. Often we need a class that contains methods that must be implemented by all derived classes but not by the base class itself. By this, actually we force the programmer to implement base class methods. Think a simple scenario. Suppose we want to develop a banking software. Here a common entity is account information. There are many type of account like savings account,  current account e.t.c in bank. In every account two things is common that is deposit and withdraw. So we can create a Account class as base class which must have this two method deposit and withdraw and it will be abstract method and the Account class will be abstract class. So, you can say – why its will be abstract class and abstract method? Answer of the first question is if there is any abstract member in the class, the class must be abstract. Answer of the second question, in every type of accounts two things is common deposit and withdraw but their way of transaction may be different. We just want to enforce the programmer to implement the methods if the create any class by inheriting the Account class

Syntax

The syntax for creating an abstract method is – use the abstract modifier with the name of the method and the parameters, followed by a semicolon by keeping it empty. It will be look like this.

[access-modifiers] abstract return-type method-name ([parameters]) ;

Example:

The following example shows how to create an abstract class Account with two abstract methods Deposit and Withdraw.

public abstract class Account
{
public abstract string Deposit(double money);
public abstract string Withdraw(double money);
}

The benefit of creating abstract methods is that it enables you to add methods to your base class that subsequently must be implemented by all derived classes, but the implementation details for these methods do not have to be defined in the base class.

Override the abstract class
When a derived class inherits an abstract method from an abstract class, it must override the abstract methods. This requirement is enforced at compile time.
The following example shows how a SavingsAccount class, which is derived from Account, uses the override keyword to implement the Deposit and Withdraw method. Here first (Account) class is base class and second (SavingsAccount) is derived class.

///
/// Base class
///
public abstract class Account
{
public abstract string Deposit(double money);
public abstract string Withdraw(double money);
}

///
/// Derived class
///
public class SavingsAccount : Account
{

//override the base class
public override string Deposit(double money)
{
return "AC type savings: Deposit transaction done sucessfully";
}

public override string Withdraw(double money)
{
return "AC type savings: Withdraw transaction done sucessfully";
}
}

After creating instance of SavingsAccount, if you call Deposit and withdraw method you will see the following output:
AC type savings: Deposit transaction done sucessfully
AC type savings: Withdraw transaction done sucessfully

Abstract class with virtual method:
You can also create an abstract class that contains virtual methods, as shown in the following example:

///
/// Base class
///

public abstract class Account
{
public virtual void BankName()
{
Console.WriteLine("Welcome to ABC Bank Limited");
}

public abstract string Deposit(double money);
public abstract string Withdraw(double money);
}

In this case, a derived class does not have to provide an implementation of the BankName method because BankName is defined as virtual. Therefore, I say the important thing again, if you have a generic method that is common to all derived classes, and you want to force each derived class to implement the method, you must define the method as abstract in the base class.

Difference between an abstract method & virtual method:

Virtual method has an implementation and provides the derived class with the option of overriding it. Abstract method does not provide an implementation and forces the derived class to override the method.

Abstract Properties:

Properties may also be declared as abstract. Same as abstract method, to declare an abstract property, specify the property name and the accessors that the derived property should implement.
Example:

///
/// Base class
///

public abstract class Account
{
//Abastract property
public abstract string AccountNo { get; set; }

//Virtual Method
public virtual void BankName()
{
Console.WriteLine("Welcome to ABC Bank Limited");
}

public abstract string Deposit(double money);
public abstract string Withdraw(double money);
}

Total Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
* Author : Md. Mahedee Hasan
* Create Date : 14/04/2012
* web : mahedee.blogspot.com
*/

namespace ConsoleProgram
{

///
/// Base class
///

public abstract class Account
{
//Abastract property
public abstract string AccountNo { get; set; }

//Virtual Method
public virtual void BankName()
{
Console.WriteLine("Welcome to ABC Bank Limited");
}

//Abstract Method
public abstract string Deposit(double money);
public abstract string Withdraw(double money);
}

///
/// Derived class
///

public class SavingsAccount : Account
{
private string acccNo;

//override the base class
public override string Deposit(double money)
{
return "AC type savings: Deposit transaction done sucessfully";
}

public override string Withdraw(double money)
{
return "AC type savings: Withdraw transaction done sucessfully";
}

//Override base class property
public override string AccountNo
{
get
{
return acccNo;
}
set
{
acccNo = value;
}
}
}

///
/// Main class
///

class Program
{
///
/// Main Method
///
///

static void Main(string[] args)
{
SavingsAccount objSavingsAccount = new SavingsAccount();
objSavingsAccount.BankName();
objSavingsAccount.AccountNo = "0110";
Console.WriteLine("Account Number: " + objSavingsAccount.AccountNo);
string msg = objSavingsAccount.Deposit(1000);
Console.WriteLine(msg);
msg = objSavingsAccount.Deposit(500);
Console.WriteLine(msg);

}
}
}

Output:
Welcome to ABC Bank Limited
Account Number: 0110
AC type savings: Deposit transaction done sucessfully
AC type savings: Deposit transaction done sucessfully

At a glance:
· An abstract class is a generic base class.
– Contains abstract methods that must be implemented by the derived class.
· Cannot create instance of abstract class.
· Abstract class can contain no abstract members.

Please share, if it is helpfulShare on FacebookShare on LinkedInTweet about this on TwitterShare on Google+Email this to someonePrint this page

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>