I have an Abstract class Animal, which stores some common fields, e.g name, health. I have a number of animal classes e.g Tiger, but I also have a class Fish which has an additional field the other animal classes don't have, canSplash
.
I then have a list of Animal objects. I can access the common fields, but I cannot access the canSplash
field for Fish. I am looking for help to accessing a concrete classes specific fields from an Abstract Class.
class Zoo
{
public List<Animal> animals = new List<Animal>();
public Zoo()
{
animals.Add(new Monkey());
animals.Add(new Tiger());
animals.Add(new Fish());
}
public static void displayZooPopulation()
{
foreach (var a in animals)
{
if (a.species == "fish" && a.CanSplash)
{
Console.WriteLine("{0} can splash",a.Name);
}
}
}
}
class Fish : Animal {
private bool canSplash
public bool CanSplash { get; set; }
}