Bird
0
0

You want to create a class ElectricCar that has all features of Car plus a new method ChargeBattery(). Which is the best way to do this using inheritance?

hard🚀 Application Q15 of 15
C Sharp (C#) - Inheritance
You want to create a class ElectricCar that has all features of Car plus a new method ChargeBattery(). Which is the best way to do this using inheritance?
AMake ElectricCar inherit Car and add ChargeBattery() method
BCopy all Car code into ElectricCar and add ChargeBattery()
CMake Car inherit ElectricCar and add ChargeBattery()
DCreate ElectricCar without inheriting Car and add ChargeBattery()
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance for extending features

    Inheritance lets ElectricCar reuse Car's features and add new ones like ChargeBattery().
  2. Step 2: Evaluate options for best practice

    Make ElectricCar inherit Car and add ChargeBattery() method correctly uses inheritance to extend Car. Copying code duplicates work. Making Car inherit ElectricCar reverses the logic. Not inheriting loses reuse.
  3. Final Answer:

    Make ElectricCar inherit Car and add ChargeBattery() method -> Option A
  4. Quick Check:

    Extend with inheritance, add new methods [OK]
Quick Trick: Extend existing class, add new methods in child [OK]
Common Mistakes:
MISTAKES
  • Copy-pasting code instead of inheriting
  • Reversing inheritance direction
  • Not using inheritance to reuse code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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