Question: C# program help Task Description public abstract class Animal { public string Name { get; set; } public abstract void MakeSound ( ) ; public

C# program help
Task Description public abstract class Animal
{
public string Name { get; set; }
public abstract void MakeSound();
public virtual void Eat()
{
Console.WriteLine("This animal eats food.");
}
}
// Example derived class
public class Bird : Animal
{
public override void MakeSound()
{
Console.WriteLine("Chirp");
}
public override void Eat()
{
Console.WriteLine("This bird eats seeds.");
}
}
// Main method
static void Main(string[] args)
{
List
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 1: 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,
e.g., "This animal eats food."
Step 2: Create Derived Classes
Create three derived classes that inherit from Animal:
Override MakeSound () For example to print a bird-specific sound, e.g.,
"Chirp".
Optionally, override Eat () to print a bird-specific eating habit, e.g., "This
bird eats seeds."
Step 3: 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.
C# program help Task Description public abstract

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!