I am doing my homework in C# in which I dealing with abstract class.
public abstract class Account
{
public abstract bool Credit(double amount);
public abstract bool Debit(double amount);
}
public class SavingAccount : Account
{
public override bool Credit(double amount)
{
bool temp = true;
temp = base.Credit(amount + calculateInterest());
return temp;
}
public override bool Debit(double amount)
{
bool flag = true;
double temp = getBalance();
temp = temp - amount;
if (temp < 10000)
{
flag = false;
}
else
{
return (base.Debit(amount));
}
return flag;
}
}
When I call the base.Debit() or base.Credit() then it gives me an error cannot call an abstract base member
.
You cannot call abstract methods, the idea is that a method declared abstract simply requires derived classes to define it. Using base.Debit
is in affect trying to call an abstract method, which cannot be done. Reading your code more closely, I think this is what you wanted for Debit()
public abstract class Account
{
protected double _balance;
public abstract bool Credit(double amount);
public abstract bool Debit(double amount);
}
public class SavingAccount : Account
{
public double MinimumBalance { get; set; }
public override bool Debit(double amount)
{
if (amount < 0)
return Credit(-amount);
double temp = _balance;
temp = temp - amount;
if (temp < MinimumBalance)
{
return false;
}
else
{
_balance = temp;
return true;
}
}
public override bool Credit(double amount)
{
if (amount < 0)
return Debit(-amount);
_balance += amount;
return true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With