Bird
Raised Fist0

You want to design a system where all vehicles must have a method StartEngine(), but the way engines start differs by vehicle type. Which approach is best in C#?

hard🚀 Application Q15 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
You want to design a system where all vehicles must have a method StartEngine(), but the way engines start differs by vehicle type. Which approach is best in C#?
ACreate a static class Vehicle with static StartEngine() method.
BCreate a concrete class Vehicle with a fully implemented StartEngine() method for all vehicles.
CUse an interface with a concrete StartEngine() method and inherit it in all vehicle classes.
DCreate an abstract class Vehicle with abstract method StartEngine(), then implement it in subclasses.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze requirement for different implementations

    Since StartEngine() differs by vehicle type, it should be declared abstract to force subclasses to provide their own version.
  2. Step 2: Choose correct class design

    An abstract class Vehicle with an abstract StartEngine() method fits best, allowing subclasses to implement specific behavior.
  3. Final Answer:

    Create an abstract class Vehicle with abstract method StartEngine(), then implement it in subclasses. -> Option D
  4. Quick Check:

    Abstract class for common plan, concrete for specifics [OK]
Quick Trick: Abstract method for varying behavior, concrete class for details [OK]
Common Mistakes:
MISTAKES
  • Using concrete class with one method for all vehicles
  • Trying to put method body in interface (not allowed)
  • Using static class which can't be inherited

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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