Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call an abstract member

Tags:

c#

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.

like image 819
Billz Avatar asked Sep 17 '25 17:09

Billz


1 Answers

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;
  }
}
like image 101
payo Avatar answered Sep 20 '25 07:09

payo