UdayK Kumar wrote:
Hi,
I am very consfused about the term "Program to an interface, not an implementation" in java.
Could you please elaborate and explain me detail
Thanks
Uday
By Programing to Interfaces your application/code is always open for modification without much code change. In Software Development process more time is spent on maintaining the code rather than its development and during this period there may be requirement of new classes.
Like if you coded to concrete classes then your code will look something like this
But if you program to Interfaces then it can be made generic
Now if there is any new addition of any foodItem/Beverages then its a lot easier for you to process it as the method
processFood(FoodItem foodItem) takes an implementation of FoodItem. On the other hand if you have programmed to concrete classes like the code given at first then you have to write new method for every food type.
Hope it makes clear that why programming to interfaces is more useful as compared to program to concrete classes.