File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Program to an interface, not an implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Program to an interface, not an implementation" Watch "Program to an interface, not an implementation" New topic
Author

Program to an interface, not an implementation

UdayK Kumar
Greenhorn

Joined: Aug 05, 2009
Posts: 26

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
Siva Prasad Reddy Katamreddy
Ranch Hand

Joined: Jun 06, 2007
Posts: 32

Hi,

Programming to Interfaces rather than concrete implementations is a good design practice.

While developing an application we will divide the whole system into sub-systems and one sub-system will talk to other sub-system to complete the whole task.
Each sub-system is separated in such-way that it should be responsible for certain kind of tasks.

So when you design the architecture of a system, first we need to divide the whole system into several sub-systems and we need to clearly define the responsibilities of each sub-system without bothering about how to implement those responsibilities. In this way we develop a prototype/blueprint of our system to be developed.

Once we have a prototype, then we need to turn these design into coding. To do this we need to use Interfaces rather than classes as we don't know the actual implementations.
And also this approach will expose the contract between the sub-systems on how to interact with each other.

Later based on the technology we chose we can provide the concrete implementations for the interfaces without affecting the design of our system.

For example, Once we have an interface with all the operation to be performed on Customer, we can choose JDBC/Hibernate/IBatis to implement the actual behavior.
Later if we want to change the Persistent handler to JPA we can change without affecting the caller code.

Thanks,
Siva
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
It means if you have a group of related classes which can have an hierarchy then make use of interfaces.

Like

interface Animal{
public void makeSound();
}

class Dog implements Anilal{
public void makeSound(){
//print dog;
}
}

class Cat implements Anilal{
public void makeSound(){
//print cat;
}
}

When you have to use the dog and cat classes use them using the Animal interface.

It helps in many thing search more about.

Also see the Java Collection framework API to understand it more.


Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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.


My Blog SCJP 5 SCWCD 5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

think of a hardware analogy (probably not a perfect one, but close enough).

would you rather have every MP3 player on the market all connect to your computer via a proprietary hardware jack or a standard USB type connector?

think about it...if you had to re-configure your pc when you change from a iPod to a Zune (just go with it), you'd think a long time before making any change. Even upgrading from a iPod Nano to an iPod Touch would require hardware reconfiguration.

OR...everything uses a USB. you can swap out MP3 players all day long, and you never have to touch your PC's hardware configuration.

think of 'programming to an interface' as a USB, and 'programming to an implementation' as a proprietary jack.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

just wanted to say thats a nice analogy fred ;)

the actual usb plug/port being the interface, and however apple/m$ actually use that as the implementation


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program to an interface, not an implementation