Question: In C# , In this exercise, you will create a simple simulation of an animal kingdom using classes, inheritance, and polymorphism in C# . The
In C# In this exercise, you will create a simple simulation of an animal kingdom using classes, inheritance, and polymorphism in C# The goal is to practice designing base and derived classes, understand the concept of method overriding, and see polymorphism in action.
Task Description
You are tasked with creating a program that simulates basic behaviors of different types of animals in a zoo. Your program should include a base class named Animal and at least three derived classes such as Bird, Mammal, and Reptile. Each derived class should override a common method to exhibit unique behaviors.
Step : Create the Base Class
Define an abstract base class Animal with the following features:
A property Name of type string.
An abstract method MakeSound that returns void. This method will be overridden in derived classes to print the sound the animal makes.
A virtual method Eat that prints a general message about eating habits, eg "This animal eats food."
Step : Create Derived Classes
Create three derived classes that inherit from Animal:
Override MakeSound For example to print a birdspecific sound, eg "Chirp".
Optionally, override Eat to print a birdspecific eating habit, eg "This bird eats seeds."
Step : Demonstrate Polymorphism
In your Main method, demonstrate polymorphism by:
Creating an array or list of Animal objects and populating it with instances of Bird, Mammal, and Reptile.
Iterating over the collection and calling both MakeSound and Eat on each Animal object. Observe how the correct method is called based on the actual object type, not the variable type.
public abstract class Animal
public string Name get; set;
public abstract void MakeSound;
public virtual void Eat
Console.WriteLineThis animal eats food.";
Example derived class
public class Bird : Animal
public override void MakeSound
Console.WriteLineChirp;
public override void Eat
Console.WriteLineThis bird eats seeds.";
Main method
static void Mainstring args
List zoo new List
new Bird Name "Parrot"
Add more animals
;
foreach var animal in zoo
Console.WriteLineanimalName;
animal.MakeSound;
animal.Eat;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
