222k views
0 votes
Consider the following code:

public void setFurnace(OnOffAble frn){
furnace = frn;
}
Please describe (100 words MAX), why using a type like "OnOffAble" (not an implementation of furnace such as "SaharaFurnaceHL42") here satisfies the principle of "Program to an Interface, not an Implementation"

User LittleLynx
by
8.3k points

1 Answer

6 votes

Final answer:

Using the 'OnOffAble' interface in the setFurnace method rather than a specific furnace class follows the principle of programming to an interface, enhancing code flexibility, and promoting loose coupling and reusability.

Step-by-step explanation:

Using a type like "OnOffAble" instead of a specific furnace implementation (e.g., "SaharaFurnaceHL42") adheres to the principle of "Program to an Interface, not an Implementation." This principle encourages developers to depend on abstractions rather than concrete classes.

It increases flexibility by allowing the method setFurnace to accept any object that implements the OnOffAble interface, which could be any type of furnace or even other devices that can be turned on and off, thereby promoting loose coupling and greater code reusability.

User Chrisbajorin
by
8.3k points