C# - Cannot create an instance of the abstract class or interface -
this question has reply here:
“cannot create instance of abstract class or interface” c# 3 answersi getting error cannot create instance of abstract class or interface
in c# tutorial.
it failing on line: result = new account(nametext, addresstext, balance);
here class:
public abstract class business relationship : 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 alternate constructor original constructor (directly above) { } public account(string inname) : // 'this ties alternate constructor original constructor (directly above) this(inname, "not supplied", 0) { } //--------------------------------------------------------------- // properties //--------------------------------------------------------------- // * * * * * * * * //global business relationship 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 business relationship public static business relationship load(string filename) { business relationship result = null; system.io.textreader textin = null; seek { 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); } grab { homecoming null; } { if (textin != null) textin.close(); } homecoming result; } };
here interface:
public interface iaccount { // business relationship info int getaccountnumber(); string getname(); decimal getbalance(); // business relationship actions void payinfunds(decimal amount); bool withdrawfunds(decimal amount); string rudeletterstring(); }
that's definition of abstract class. cannot create instance of it, type derived it.
think of fruit. cannot have a fruit sitting on table. must type of fruit, such orange or apple. in case, fruit abstract class, , can implement functionality , properties mutual fruit. orange , apple derive fruit class, , implement functionality , properties specific type of fruit.
use abstract
keyword when not create sense create instance of class, want implement base of operations functionality can shared across derived types. if want define contract without any shared code, you'd utilize interface.
c# class oop interface
No comments:
Post a Comment