• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program to an interface, not an implementation

 
UdayK Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic