Bird
Raised Fist0

You want to add a new animal type without changing existing code that uses Animal references. How does polymorphism help achieve this?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
You want to add a new animal type without changing existing code that uses Animal references. How does polymorphism help achieve this?
ABy making Animal methods static and adding new static methods
BBy creating a new class inheriting Animal and overriding methods
CBy rewriting all code that uses Animal references
DBy modifying the existing Animal class methods directly
Step-by-Step Solution
Solution:
  1. Step 1: Use inheritance to add new animal

    Creating a new class inheriting Animal allows extending functionality without changing existing code.
  2. Step 2: Override polymorphic methods

    Overriding methods like Speak ensures new animal behaves correctly when called via Animal references.
  3. Final Answer:

    By creating a new class inheriting Animal and overriding methods -> Option B
  4. Quick Check:

    Polymorphism supports extension without modifying existing code [OK]
Quick Trick: Add new types by subclassing and overriding methods [OK]
Common Mistakes:
MISTAKES
  • Changing base class breaking existing code
  • Rewriting all client code unnecessarily
  • Using static methods preventing polymorphism

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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