Bird
0
0

You want to create a class hierarchy where all animals can make a sound, but each animal makes a different sound. How should inheritance help you design this in C#?

hard🚀 Application Q8 of 15
C Sharp (C#) - Inheritance
You want to create a class hierarchy where all animals can make a sound, but each animal makes a different sound. How should inheritance help you design this in C#?
AUse inheritance to copy all methods from Animal to derived classes without changes
BCreate separate classes for each animal without inheritance
CMake Animal class sealed to prevent inheritance
DCreate a base class Animal with a virtual method MakeSound(), then override it in each derived animal class
Step-by-Step Solution
Solution:
  1. Step 1: Understand polymorphism with inheritance

    Using a base class with a virtual method allows derived classes to provide their own implementation.
  2. Step 2: Apply design to animal sounds

    Create a base class Animal with a virtual method MakeSound(), then override it in each derived animal class correctly uses virtual method in Animal and overrides in each derived class for different sounds.
  3. Final Answer:

    Create a base class Animal with a virtual method MakeSound(), then override it in each derived animal class -> Option D
  4. Quick Check:

    Virtual method + override = flexible behavior [OK]
Quick Trick: Use virtual methods in base class for customizable behavior [OK]
Common Mistakes:
MISTAKES
  • Not using inheritance and duplicating code
  • Copying methods without overriding
  • Sealing base class prevents extension

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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