Suppose I have 2 classes (which don't show methods to look simpler):
public interface Fruit{ } public class Orange implements Fruit{ } , and assume I can use 2 ways to initialize Orange without actual difference (means both can compile and work at the same way in my code):
1.Abstract one:
Fruit obj=new Orange(); 2.Concrete one:
Orange obj=new Orange(); ,according to Understanding "programming to an interface", as I understand, I should use the abstract one, because it fits more "programming to interface" and have less coupling.
But I don't quite understand why the abstract one has less coupling ,because I think the abstract one has 2 keywords of classes : Fruit and Orange, while the concrete one has 1 keyword only : Orange, which means the abstract one depend on more classes. While I agree the abstract one may allow me to switch from Orange to other type (eg:Grape) with erasing and typing less characters, it still contains 2 keywords after switching(Fruit and Grape).
Also I found the other disadvantage of the abstract one is , if one day, Orange needs to remove the interface, or change to other interface:
public class Orange{ } or public interface CircleShape{ }
public class Orange implements CircleShape{ } , the abstract one needs to modify, while the concrete one doesn't need to do so. So I think the concrete one has less coupling than the abstract one. Is that true? If not, what is the misconception here?
objonly needs to behave as aFruit, then you have abstracted the details of its "orange-ness". IfOrangeneeds to become no longer aFruitlater on, then you will have to change the definition and usage everywhere, and abstraction no longer plays a role.