-8

I am getting the error Cannot create an instance of the abstract class or interface in a C# tutorial.

It is failing on this line: result = new Account(nameText, addressText, balance);

Here is my class:

public abstract class Account : IAccount
{
    //---------------------------------------------------------------
    // Constructor
    //---------------------------------------------------------------
    public Account(string inName, string inAddress, decimal inBalance)
    {
        name = inName;
        address = inAddress;
        balance = inBalance;
    }
    public Account(string inName, string inAddress) :
        this(inName, inAddress, 0)      // 'this ties this alternate constructor back to the original constructor (directly above)
    {
    }
    public Account(string inName) :     // 'this ties this alternate constructor back to the original constructor (directly above)
        this(inName, "Not Supplied", 0)
    {
    }


    //---------------------------------------------------------------
    // Properties
    //---------------------------------------------------------------
    //  *       *       *       *       *       *       *       *
    //global account constraints
    private static decimal minIncome = 10000;
    private static int minAge = 18;

    //  *       *       *       *       *       *       *       *
    //personal details
    private string name;
    private string address;

    //  *       *       *       *       *       *       *       *
    //account details
    public int AccountNumber;
    public static decimal InterestRateCharged;
    public AccountState State;
    private decimal balance = 0;
    public int Overdraft;


    //---------------------------------------------------------------
    // Methods
    //---------------------------------------------------------------
    // loads the account
    public static Account Load(string filename)
    {
        Account result = null;
        System.IO.TextReader textIn = null;

        try
        {
            textIn = new System.IO.StreamReader(filename);
            string nameText = textIn.ReadLine();
            string addressText = textIn.ReadLine();
            string balanceText = textIn.ReadLine();
            decimal balance = decimal.Parse(balanceText);
            result = new Account(nameText, addressText, balance);
        }
        catch
        {
            return null;
        }
        finally
        {
            if (textIn != null) textIn.Close();
        }
        return result;
    }

};

Here is my interface:

public interface IAccount
{
    // account info
    int GetAccountNumber();
    string GetName();
    decimal GetBalance();

    // account actions
    void PayInFunds(decimal amount);
    bool WithdrawFunds(decimal amount);
    string RudeLetterString();
}
sion_corn
  • 3,043
  • 8
  • 39
  • 65
  • 1
    [An abstract class cannot be instantiated](http://msdn.microsoft.com/en-gb/library/sf985hc5.aspx) – CodingIntrigue Jun 26 '14 at 17:58
  • Why is this class abstract if you want to instantiate it? – eddie_cat Jun 26 '14 at 17:59
  • 5
    What part of the error don't you understand? – SLaks Jun 26 '14 at 18:00
  • Are you sure you even read the error message? Even if you don't you can copy & paste it directly into google which will give you plenty of results explaining exactly what it is, why it happens, and how to fix it. – tnw Jun 26 '14 at 18:09

2 Answers2

14

That's the definition of an abstract class. You cannot create an instance of it, only a type derived from it.

Think of something like a fruit. You cannot have a fruit sitting on the table. It must be a certain type of fruit, such as an orange or an apple. In this case, fruit would be an abstract class, and can implement functionality and properties common to all fruit. Orange and Apple would derive from the Fruit class, and implement functionality and properties specific to that type of fruit.

Use the abstract keyword when it does not make sense to create an instance of your class, but you want to implement base functionality that can be shared across derived types. If you simply want to define a contract without any shared code, you'd use an interface.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • I think the second paragraph would be more helpful if you explained why it was good to have the concept of an abstract *fruit* rather than just the restriction that you need a concrete fruit. In particular how abstract classes differ an interface. – Conrad Frix Jun 26 '14 at 18:07
  • @Mike Christensen, thanks for the explanation. +1. – sion_corn Jun 26 '14 at 18:12
  • @ConradFrix - Thanks for the suggestions, I've updated my answer. I want to keep it somewhat simple to avoid information overload. – Mike Christensen Jun 26 '14 at 18:23
  • 2
    Speaking about abstract classes, I like this example very much. Boy I wish this had been shown me when I first encountered abstract classes. – jmodrak Jun 26 '14 at 19:30
  • 1
    +1. I like the detailed description [Mike Christensen](http://stackoverflow.com/users/392044/mike-christensen), it makes it much more detailed than normal technical documentation, and everybody can understand the concept of fruit! :) Nice job Mike, but you COULD have included a short sample of how we could have made this work, by creating an instance of the class, implementing the abstract functions marked in the class, and possibly explaining the use of VIRTUAL function in a class, setup like this... (Heh.. Just bustin' your chops!... Your description is still wonderful!!) Nice work! – LarryF Jul 04 '14 at 22:56
2
public abstract class Account : IAccount

Remove the word abstract

If you want to only be able to create an Account instance from your static method, make the constructors private or protected instead.

Matthew
  • 24,703
  • 9
  • 76
  • 110