question about abstract class/polymorphism
I am having trouble understanding the concept of abstract class while using the concept of polymorphism.
I have an understanding of certain concepts correct me if I am wrong.
INSTANCIATING AN OBJECT
public class Account()
{
//has couple of methods and contructors defined in this class
}
public class testAccount()
{
Account acc; // acc is a reference to an Account object;
// the object itself does not yet exist.
acc = new Account(); // Account object now exists and
//acc is a reference to it
}//testAccount
Is this right? Ok then when you come to the definition of Abstract class..
ABSTRACT CLASS: An abstract class is basically a template to be followed by various derived classes. for example say classes such as SavingsAccount() and CheckingAccount() are derived from the base class Account(). Also, an abstract class CANNOT be instantiated..right??
so finally my question is in this code: Please do follow the code carefully as it uses enum, polymorphism, abstract class, virtual/override concepts in this code.
//Account.cs file
abstract public class Account
{
private int id;
protected decimal balance;
private string owner;
protected int numXact = 0; // number of transactions
public Account(decimal balance, string owner, int id)
{
this.balance = balance;
this.owner = owner;
this.id = id;
}
virtual public void Deposit(decimal amount)
{
balance += amount;
numXact++;
}
virtual public void Withdraw(decimal amount)
{
balance -= amount;
numXact++;
}
public decimal Balance
{
get
{
return balance;
}
}
public int Id
{
get
{
return id;
}
}
public string Owner
{
get
{
return owner;
}
set
{
owner = value;
}
}
public int Transactions
{
get
{
return numXact;
}
}
virtual public string GetStatement()
{
string s = "Statement for " + this.Owner + " id = " +
+ Id + "\n" +
this.Transactions + " transactions, balance = " + balance;
return s;
}
abstract public void Post();
abstract public string Prompt {get;}
virtual public void MonthEnd()
{
numXact = 0;
}
}
//CheckingAccount.cs
using System;
public class CheckingAccount : Account
{
private decimal fee = 5.00m;
private const int FREEXACT = 2;
public CheckingAccount(decimal balance, string owner, int id)
: base(balance, owner, id)
{
}
public decimal Fee
{
get
{
if (numXact > FREEXACT)
return fee;
else
return 0.00m;
}
}
override public string GetStatement()
{
string s = base.GetStatement();
s += ", fee = " + Fee;
return s;
}
override public void Post()
{
balance -= Fee;
}
override public string Prompt
{
get
{
return "C: ";
}
}
}
//SavingsAccount.cs
using System;
public class SavingsAccount : Account
{
private decimal minBalance;
private decimal rate = 0.06m;
public SavingsAccount(decimal balance, string owner, int id)
: base(balance, owner, id)
{
minBalance = balance;
}
public decimal Interest
{
get
{
return minBalance * rate/12;
}
}
override public void Withdraw(decimal amount)
{
base.Withdraw(amount);
if (balance < minBalance)
{
minBalance = balance;
}
}
override public void Post()
{
balance += Interest;
}
override public string GetStatement()
{
string s = base.GetStatement();
s += ", interest = " + Interest;
return s;
}
public decimal Rate
{
get
{
return rate;
}
set
{
rate = value;
}
}
override public string Prompt
{
get
{
return "S: ";
}
}
override public void MonthEnd()
{
base.MonthEnd();
minBalance = balance;
}
}
//Bank.cs
using System;
public enum AccountType
{
Checking,
Savings,
Invalid
}
public class Bank
{
private Account[] accounts;
private int nextid = 1;
private int count = 0;
public Bank()
{
accounts = new Account[10]; //so how can one instantiate an abstract class??
AddAccount(AccountType.Checking, 100, "Bob");
AddAccount(AccountType.Savings, 200, "Mary");
AddAccount(AccountType.Checking, 300, "Charlie");
}
public int AddAccount(AccountType type, decimal bal, string owner)
{
Account acc;
int id = nextid++;
switch(type)
{
case AccountType.Checking:
acc = new CheckingAccount(bal, owner, id);
break;
case AccountType.Savings:
acc = new SavingsAccount(bal, owner, id);
break;
default:
Console.WriteLine("Unexpected AccountType");
return -1;
}
accounts[count++] = acc;
return id;
}
public string[] GetAccounts()
{
string[] array = new string[count];
for (int i = 0; i < count; i++)
{
string owner = accounts[i].Owner;
string stype = accounts[i].Prompt;
string sid = accounts[i].Id.ToString();
string sbal = accounts[i].Balance.ToString();
string str = sid + "\t" + stype + owner + "\t" + sbal;
array[i] = str;
}
return array;
}
public void DeleteAccount(int id)
{
int index = FindIndex(id);
if (index != -1)
{
// move accounts down
for (int i = index; i < count; i++)
{
accounts[i] = accounts[i+1];
}
count--;
}
}
private int FindIndex(int id)
{
for (int i = 0; i < count; i++)
{
if (accounts[i].Id == id)
return i;
}
return -1;
}
public Account FindAccount(int id)
{
for (int i = 0; i < count; i++)
{
if (accounts[i].Id == id)
return accounts[i];
}
return null;
}
}
For the convenience of the friends try to help me in understanding this concept I am reiterating the question.
How can an abstract class object be instantiated in the Bank class.
|