Valerie Meunier

Valerie Meunier

  • 1.7k
  • 465
  • 21.2k

Problem with interface

Jun 11 2022 5:50 PM

Hi

there are two kinds of bank accounts: normal account and saving account.For each, a diiferent method Debit is applied.

I'm trying to implement a method for both accounts. I have already done it with absctract class, but now i want to do it with using interface.

Class Bank is ok.

The first error i meet is public class SavingAccount : Bank 'base class Bank is less accessible than class SavingAccount.

The next error is at  public override void Debit(double amount): cannot override inherited member because it's not marked as abstract ... But if I set abstract at line  public abstract void Debit(double amount) in class Bank, i get the error: "cannot declare a body because it's abstract.

Thanks

using System;
interface IBank
{
    void Debit(double amount);
}

class Bank : IBank
{
    private string owner;
    private double balance;

    public string Owner
    {
        get { return owner; }
    }

    public double Balance
    {
        get { return balance; }
        set { balance = value; }
    }

    public string See()
    {
        string descr = balance.ToString();
        return descr;
    }

    public Bank(string theOwner, double theBalance)
    {
        owner = theOwner;
        balance = theBalance;
    }

    public void Debit(double amount)
    {

    }
}


public class SavingAccount : Bank
{
    public SavingAccount(string theOwner, double theBalance) : base(theOwner, theBalance)
    {

    }
    public override void Debit(double amount)
    {
        Balance -= amount - 5;
    }
}

public class NormalAccount : Bank
{
    public NormalAccount(string theOwner, double theBalance) : base(theOwner,theBalance)
    {

    }
    public override void Debit(double amount)
    {
            Balance -= amount - 10;
    }
}

public class Program
{
    static void Main(string[] args)
    {
    SavingAccount saveBob = new SavingAccount("Bob", 1000);
    saveBob.Debit(400);
    Console.WriteLine(saveBob.See());

    NormalAccount normalBob = new NormalAccount("Bob", 200);
    normalBob.Debit(200);
    Console.WriteLine(saveBob.See());
    }
}


Answers (3)