Bird
0
0

Which of the following is the correct syntax to override a virtual method named Display in a derived class?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Inheritance
Which of the following is the correct syntax to override a virtual method named Display in a derived class?
Apublic void Display() { }
Bpublic virtual void Display() { }
Cpublic new void Display() { }
Dpublic override void Display() { }
Step-by-Step Solution
Solution:
  1. Step 1: Identify override syntax

    To change a virtual method in a derived class, use override before the method signature.
  2. Step 2: Eliminate other options

    The plain public void Display() { } lacks the override keyword, public virtual void Display() { } incorrectly uses virtual in the derived class, and public new void Display() { } hides the base method but doesn't override it for polymorphism.
  3. Final Answer:

    public override void Display() { } -> Option D
  4. Quick Check:

    Override method uses override keyword [OK]
Quick Trick: Override methods must use override keyword [OK]
Common Mistakes:
MISTAKES
  • Omitting override keyword in derived class
  • Using virtual instead of override in derived class
  • Using new keyword instead of override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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