Complete the code to declare a base class named Animal.
public class [1] { public virtual void Speak() { Console.WriteLine("Animal sound"); } }
The base class should be named Animal as it represents the general class.
Complete the code to declare a derived class Dog that inherits from Animal.
public class Dog : [1] { public void Bark() { Console.WriteLine("Woof!"); } }
The derived class Dog inherits from the base class Animal.
Fix the error in the code to call the Speak method from the Dog class instance.
Dog myDog = new Dog();
myDog.[1]();The Speak method is defined in the base class Animal and can be called on the Dog instance.
Fill both blanks to override the Speak method in the Dog class.
public class Dog : Animal { public override void [1]() { Console.WriteLine([2]); } }
To override the base class method, use Speak and print "Woof!" for the Dog class.
Fill all three blanks to create a derived class Cat that overrides Speak and adds a new method Purr.
public class Cat : [1] { public override void [2]() { Console.WriteLine("Meow!"); } public void [3]() { Console.WriteLine("Purr..."); } }
The class Cat inherits from Animal, overrides Speak, and adds a new method Purr.