c# - Overriding toSting in derived class -
i have abstract class animal
looking this
abstract class animal{ protected int numberofpaws; public abstract string speak(); public string tostring(){ return "i have " + numberofpaws + " paws"; } }
then have derived class has new implementation of tostring()
looking this
class cow : animal{ public cow : base(4) {} public override string speak(){ return "moo!"; } public new strinf tostring(){ return "i cow " + base.tostring(); } }
and have main looking this
void main{ cow c = new cow(); console.writeline(c.tostring()); }
but when run prints userquery+cow
console.
i have no clue i'm doing wrong? doing beacause of base.tostring()
?
use override
keyword:
abstract class animal { protected int numberofpaws; public abstract string speak(); public override string tostring(){ return "i have " + numberofpaws + " paws"; } } class cow : animal { public cow : base(4) {} public override string speak(){ return "moo!"; } public override string tostring(){ return "i cow " + base.tostring(); } }
Comments
Post a Comment