Bird
0
0

Which code correctly uses base to achieve this?

hard🚀 Application Q8 of 15
C Sharp (C#) - Inheritance
You have a base class Vehicle with a virtual method Start(). You want to create a derived class Car that calls the base Start() method and then prints "Car started". Which code correctly uses base to achieve this?
Apublic new void Start() { base.Start(); Console.WriteLine("Car started"); }
Bpublic void Start() { base.Start(); Console.WriteLine("Car started"); }
Cpublic override void Start() { Console.WriteLine("Car started"); base.Start(); }
Dpublic override void Start() { base.Start(); Console.WriteLine("Car started"); }
Step-by-Step Solution
Solution:
  1. Step 1: Use override to extend base virtual method

    Since Vehicle.Start() is virtual, Car must override it to extend behavior.
  2. Step 2: Call base.Start() first, then print message

    public override void Start() { base.Start(); Console.WriteLine("Car started"); } correctly overrides Start(), calls base.Start(), then prints "Car started".
  3. Final Answer:

    public override void Start() { base.Start(); Console.WriteLine("Car started"); } -> Option D
  4. Quick Check:

    Override virtual method and call base first [OK]
Quick Trick: Override virtual methods and call base.Method() to extend [OK]
Common Mistakes:
MISTAKES
  • Using new instead of override
  • Calling base after derived code
  • Not overriding virtual method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes