Bird
0
0

You want to design a system where different types of vehicles share a common method startEngine() but each vehicle starts differently. Which approach using abstract classes is best?

hard📝 Application Q15 of 15
Java - Abstraction
You want to design a system where different types of vehicles share a common method startEngine() but each vehicle starts differently. Which approach using abstract classes is best?
AMake Vehicle a concrete class with startEngine() implemented, subclasses override it if needed.
BMake an abstract class Vehicle with an abstract method startEngine(), then subclasses implement it.
CMake Vehicle an interface with startEngine() method, implemented by subclasses.
DMake Vehicle a final class with startEngine() method.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the need for shared method with different implementations

    Since startEngine() must be shared but implemented differently, an abstract method enforces subclasses to provide their own version.
  2. Step 2: Choose abstract class with abstract method

    Declaring Vehicle as abstract with abstract startEngine() ensures all subclasses implement it, sharing the concept but customizing behavior.
  3. Final Answer:

    Make an abstract class Vehicle with an abstract method startEngine(), then subclasses implement it. -> Option B
  4. Quick Check:

    Abstract class with abstract method enforces implementation [OK]
Quick Trick: Use abstract method to force subclass-specific behavior [OK]
Common Mistakes:
  • Using concrete method without forcing override
  • Confusing interfaces with abstract classes
  • Making class final prevents subclassing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes