Saturday, 22 December 2012 13:54

c# abstract class

Written by 
Rate this item
(0 votes)

The abstract class indicates that a class may be used
as a base class for other classes and is not instantiable.

so Abstract classes look similar like base classes,not
allowing to generate instances of them.Defining a class
as abstract,we can make it secure.so c# provides secure
OOP programming.
look at the following example-
//base class
abstract public class BankAccount{
private string customerName;
private string accountNumber;
private double banlance;
public BankAccount(string accountNumber, string customerName)
: this()
{
this.accountNumber = accountNumber;
this.customerName = customerName;
}
public string Deposit(double amount){ //codes }
public virtual string Withdraw(double amount{ //codes }
//derived class
public class SavingsAccount: BankAccount{
private double interestRate;
public SavingsAccount(string accountNumber, string customerName,
double interestRate) : base(accountNumber, customerName)
{
this.interestRate = interestRate;
}
public override string Withdraw(double amount){ //codes }
}
public class MainClass{
public static void main(){
BankAccount svAcc=new BankAccount("Sv-203736","mridul");//wrong
SavingsAccount svAcc2 = new SavingsAccount("Sv-203736",
"mridul", 5.5);

in the first line of the main method in the above example,we
can't initiate BankAccount object because it was declared as
abstract so it's not instantiable.we can use the functionality
of that base class by the derived classes like SavingsAccount
class in the above example.we can have the private members,
constructors,and methods from the base class-BankAccount to
the derived class SavingsAccount.In this way c# programs are
protected.

Read 4565 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.