Complete the code to declare a base class named Animal.
public class [1] { public void Speak() { Console.WriteLine("Animal sound"); } }
The base class should be named Animal to represent a general animal.
Complete the code to override the Speak method in the Dog class.
public class Dog : Animal { public override void [1]() { Console.WriteLine("Bark"); } }
The method Speak is overridden to provide a specific sound for Dog.
Fix the error in the code by completing the method declaration to allow polymorphism.
public class Animal { public [1] void Speak() { Console.WriteLine("Animal sound"); } }
static which prevents overriding.sealed which prevents inheritance.abstract without making the class abstract.The virtual keyword allows derived classes to override the method, enabling polymorphism.
Fill both blanks to create a list of animals and call their Speak method polymorphically.
List<[1]> animals = new List<[2]> { new Dog(), new Cat() }; foreach(var animal in animals) { animal.Speak(); }
The list should be of type Animal to hold different animal objects and call their overridden methods.
Fill all three blanks to implement polymorphism with an interface and classes.
public interface I[1] { void Speak(); } public class [2] : I[1] { public void Speak() { Console.WriteLine("Meow"); } } I[1] animal = new [2](); animal.Speak();
The interface is named IAnimal, the class Cat implements it, and the variable uses the interface type to call Speak polymorphically.