Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 211

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?

    Correct Answer: A

    The most optimized approach to meet the requirements would be creating an abstract class 'Vehicle' with concrete implementations for 'start()' and 'stop()', and declaring 'ride()' as an abstract method. This allows 'Bus' and 'Boat' classes to inherit the common functionality of 'start()' and 'stop()' from 'Vehicle' and provides their unique implementations of 'ride()'. This approach follows the principles of object-oriented programming by maximizing code reuse and ensuring that common functionality is maintained in the base class, while allowing specific behavior to be defined in the subclasses.

Discussion
EricausdresdenOption: A

But why is it not Answer A, though?

SyyyyyyyyOption: A

Apparently it's A. Both A and D can work, but default methods in interfaces are introduced to provide extension to interfaces, not really to use it to provide abstraction.

charbelishakOption: A

both A and D are correct. in A, we define an abstract class with concrete methods for start() and stop(), and an abstract ride(). in B, we define default start() and stop(), and ride() we keep it abstract. meaning in both approaches we are defining a common operation, and require an overridden version for ride() for the code to work. the only difference is if we want to extend another class other than Vehicle, then we need option D, however with the requirements we have here, both options works fine.

kingofkothaOption: A

option A is right answer.

UAK94Option: D

interface Vehicle{ default void start() {} default void stop() {} default void ride() {} } class Bus implements Vehicle{ public void ride() {System.out.println("Bus");} } public class Boat implements Vehicle{ public void ride() {System.out.println("Boat");} } Answer is D

iSnoverOption: D

The answer is the letter D, the question makes it clear that the "start()", "stop()" and "ride()" methods all classes need to have, so Vehicle needs to be an interface to be optimized, with that we eliminate alternative A and C. The letter B is also correct but it is not as optimized as D, because all the methods of an interface are by default public and abstract, you don't need to declare this when instantiating the method, you just need to overload it.

yyfabcOption: A

It feels weird to me ... if i have to solve this problem in a realistic scenario, i think i could create abstract class with implementations of those 2 methods and make ride() abstract, or using default in interface to put fixed logic for the 2 methods and let Bus and Boat implement the interface and override ride().. plz correct me for any mistake, tx!