I recently came across a problem related to class creation/modeling.
Model animal kingdom as classes. Use your classes to create various objects represented as a virtual zoo. Add 20 animals to this virtual zoo (in your main method of your program and make let each animal make a sound. This should not be 20 different species).
I thought about the problem and decided to go with an abstract class Animal rather than an interface. Then i added two classes Bird, Mammal as inherited them by Animal.
My implementation
public abstract class Animal{
abstract Sound MakeSound();
}
public class Bird : Animal
{
private Sound _sound;
public Bird(Sound sound){
_sound = sound;
}
public Sound MakeSound(){
return _sound;
}
}
//same as above with Mammals
public class Program
{
Animal crow = new Bird("Crow sound");
Console.WriteLine(crow.MakeSound());
Animal dog = new Mammal("Dog sound");
Console.WriteLine(dog.MakeSound());
// and so on with other animals
}
My questions are -
- What is wrong with my implementation?
- What would be the correct way to design the animal kingdom as per oops concepts?
- In what scenario do we use an interface over abstract class and vice versa. (with real world example)
Kindly suggest some bang on online material where i can improve on such problems. Thanks.