How does polymorphism help you when you want to draw all shapes without checking their types?
hard🚀 Application Q15 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
You have a list of different shapes (Circle, Square, Triangle) all inheriting from Shape with a virtual method Draw(). How does polymorphism help you when you want to draw all shapes without checking their types?
AYou can call Draw() on each Shape reference and the correct shape's Draw runs automatically.
BYou must check each shape's type and call its specific Draw method manually.
CYou need to cast each shape to its exact type before calling Draw().
DYou cannot use polymorphism with collections of different shapes.
Step-by-Step Solution
Solution:
Step 1: Understand polymorphism with collections
Polymorphism allows calling the same method on base class references that point to different derived objects.
Step 2: Apply to drawing shapes
Calling Draw() on each Shape in the list runs the correct overridden Draw method for each shape automatically.
Final Answer:
You can call Draw() on each Shape reference and the correct shape's Draw runs automatically. -> Option A
Quick Check:
Polymorphism enables method calls without type checks [OK]
Quick Trick:Call base method; derived version runs automatically [OK]
Common Mistakes:
MISTAKES
Thinking you must check types before calling methods
Trying to cast objects unnecessarily
Believing polymorphism doesn't work with lists
Master "Polymorphism and Abstract Classes" in C Sharp (C#)
9 interactive learning modes - each teaches the same concept differently