Oracle
1Z0-850 · Question #7
Click the Exhibit button. Which, inserted at line 3 of the TestDrink class, demonstrates the "program to an interface" principle? 1. interface Drinkable { 2. void drink(); 3. } 1. class Tea…
The correct answer is A. Drinkable aDrink = DrinkableFactory.getDrinkable(). See the full explanation below for the reasoning.
Question
Click the Exhibit button. Which, inserted at line 3 of the TestDrink class, demonstrates the "program to an interface" principle?
- interface Drinkable {
- void drink();
- }
- class Tea implements Drinkable {
- public void drink() { System.out.println("Drinking tea..."); }
- }
- class Coffee implements Drinkable {
- public void drink() { System.out.println("Drinking coffee..."); }
- }
- public class TestDrink {
- static boolean teaflag;
- public static Drinkable getDrinkable() {
- if (teaflag) {
- return new Tea();
- } else {
- return new Coffee();
- }
- }
- // more code that sets the value of the teaflag boolean
- }
- public class DrinkableFactory {
- static boolean teaflag;
- public static Drinkable getDrinkable() {
- if (teaflag) {
- return new Tea();
- } else {
- return new Coffee();
- }
- }
- // more code that sets the value of the teaflag boolean
- }
Options
- ADrinkable aDrink = DrinkableFactory.getDrinkable();
- BDrinkable aDrink = new Drinkable();
- CTea aDrink = new Drinkable();
- DTea aDrink = new Tea();
How the community answered
(56 responses)- A80% (45)
- B5% (3)
- C11% (6)
- D4% (2)
Community Discussion
No community discussion yet for this question.