Reputation: 1268
I have an abstract class Animal
Looking like this
abstract class Animal{
protected int numberOfPaws;
public abstract String speak();
public String toString(){
return "I have " + numberOfPaws + " paws";
}
}
Then I have a derived class that has a new implementation of toString()
looking like this
class Cow : Animal{
public Cow : base(4) {}
public override String speak(){
return "Moo!";
}
public new Strinf toString(){
return "I am a Cow " + base.toString();
}
}
And I have a main looking like this
void Main{
Cow c = new Cow();
Console.WriteLine(c.toString());
}
But when I run this it prints UserQuery+Cow
to the console.
I have no clue as to what i'm doing wrong? Is it doing this beacause of the base.toString()
?
Upvotes: 0
Views: 63
Reputation: 71
You should read here : How To: Override the ToString Method
public override string ToString()
{
return "I have " + numberOfPaws + " paws";
}
You miss the override
keyword
Upvotes: 2
Reputation: 2761
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 am a Cow " + base.ToString();
}
}
Upvotes: 3