Bird
0
0

What is wrong with this C# code snippet that tries to use polymorphism?

medium📝 Debug Q14 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What is wrong with this C# code snippet that tries to use polymorphism?
class Shape { public void Draw() { Console.WriteLine("Shape"); } }
class Circle : Shape { public void Draw() { Console.WriteLine("Circle"); } }

Shape s = new Circle();
s.Draw();
AShape cannot be assigned a Circle object.
BDraw method in Shape should be virtual to enable polymorphism.
CCircle class must not have a Draw method.
DDraw method must be static.
Step-by-Step Solution
Solution:
  1. Step 1: Check method declarations for polymorphism

    Polymorphism requires the base method to be marked 'virtual' and the derived method to 'override'.
  2. Step 2: Identify missing virtual keyword

    Here, Shape's Draw is not virtual, so Circle's Draw hides it but does not override.
  3. Final Answer:

    Draw method in Shape should be virtual to enable polymorphism. -> Option B
  4. Quick Check:

    Base method must be virtual for polymorphism [OK]
Quick Trick: Base method needs 'virtual' for polymorphism [OK]
Common Mistakes:
MISTAKES
  • Thinking method hiding equals polymorphism
  • Believing derived method must be removed
  • Assuming static methods support polymorphism

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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