Complete the code to declare a method that can be overridden in a derived class.
public class Animal { public [1] void Speak() { Console.WriteLine("Animal sound"); } }
The virtual keyword allows a method to be overridden in a derived class.
Complete the code to override the Speak method in the Dog class.
public class Dog : Animal { public [1] void Speak() { Console.WriteLine("Bark"); } }
The override keyword is used in the derived class to provide a new implementation of a virtual method.
Fix the error in the derived class method declaration to correctly override the base method.
public class Cat : Animal { public [1] void Speak() { Console.WriteLine("Meow"); } }
To override a virtual method from the base class, the derived method must use the override keyword.
Fill both blanks to declare a virtual method in the base class and override it in the derived class.
public class Bird { public [1] void Fly() { Console.WriteLine("Flying"); } } public class Sparrow : Bird { public [2] void Fly() { Console.WriteLine("Sparrow flying fast"); } }
The base class method must be virtual to allow overriding, and the derived class method must use override to replace it.
Fill all three blanks to override a virtual method and call the base method inside the override.
public class Vehicle { public [1] void Start() { Console.WriteLine("Vehicle starting"); } } public class Car : Vehicle { public [2] void Start() { [3].Start(); Console.WriteLine("Car starting with extra steps"); } }
The base method is declared virtual. The derived method uses override and calls the base method with base.