Java SE 8 Programmer I — Question 149
Given these requirements:
✑ Bus and Boat are Vehicle type classes.
✑ The start() and stop() methods perform common operations across the Vehicle class type.
✑ The ride() method performs a unique operations for each type of Vehicle.
Which set of actions meets the requirements with optimized code?
Answer options
- A. 1. Create an abstract class Vehicle by defining start() and stop() methods, and declaring the ride() abstract method. 2. Create Bus and Boat classes by inheriting the Vehicle class and overriding the ride() method.
- B. 1. Create an interface Vehicle by defining start() and stop() methods, and declaring the ride() abstract method. 2. Create Bus and Boat classes by implementing the Vehicle class.
- C. 1. Create an abstract class Vehicle by declaring stop(), start(), and ride() abstract methods. 2. Create Bus and Boat classes by inheriting the Vehicle class and overriding all the methods.
- D. 1. Create an interface Vehicle by defining default stop(), start(), and ride() methods. 2. Create Bus and Boat classes by implementing the Vehicle interface and overriding the ride() method.
Correct answer: A
Explanation
The correct answer is A because it effectively utilizes an abstract class to define common methods (start() and stop()) while allowing the ride() method to be uniquely implemented in the Bus and Boat classes. Option B incorrectly uses an interface, which does not allow for the definition of non-abstract methods. Option C is incorrect because it declares all methods as abstract, which is unnecessary for the common operations. Option D misuses an interface by declaring default methods, which is not aligned with the requirement for abstract functionality.