• Post Reply Bookmark Topic Watch Topic
  • New Topic

Programming to Interface  RSS feed

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

Hi,

Can anyone expalin me, what is the use and purpose of Programming to interface with proper example?

#1

List list = new ArrayList(); //Good Programming

#2
ArrayList list = new ArrayList(); //Bad Programming

Why #1 is good, but not #2?

Please explain me with sample code.....


Thanks
Uday
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick search for "programming to interface" through these forums (which you should have done - SearchFirst) finds this: http://www.coderanch.com/t/383836/Java-General/java/Programming-Interfaces
 
Paul Gregory Raj Viagulam Brema
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coding to interface helps us to change the implentation as and when it is needed to any other implementation.

For an Example:
You have a requirement like this:

List list1 = new ArrayList();Later you need the same reference to be changed to some other implementation, say "LinkedList"

Now you can change ArrayList to LinkedList Implentation

list1 = new LinkedList();
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not *always* bad to code to an implementation--if you have a *reason* for needing to use an ArrayList (like performance, or if you're coding to a custom List implementation, etc.) then it might be fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!