• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diffrence between pointing the List to an arrayList instance and ArrayList to an arrayList instance.  RSS feed

 
naveen sakhare
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can Someone please explain me the difference between.
List list = new ArrayList();
ArraList list=new ArrayList();

I am not able to figure it out .
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the difference between "programming to the interface" vs. "programming to the implementation", which you can read about in http://www.coderanch.com/t/490447/java/java/Program-interface-implementation and http://www.dreamincode.net/forums/topic/39833-programming-to-an-interface-not-an-implementation/, amongst many other places.
 
naveen sakhare
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That's the difference between "programming to the interface" vs. "programming to the implementation", which you can read about in http://www.coderanch.com/t/490447/java/java/Program-interface-implementation and http://www.dreamincode.net/forums/topic/39833-programming-to-an-interface-not-an-implementation/, amongst many other places.


Thanks for that.But this one made it clear to me



The specific example I used to give to students is that they should write

List myList = new ArrayList(); // programming to the List interface

instead of

ArrayList myList = new ArrayList(); // this is bad

These look exactly the same in a short program, but if you go on to use myList 100 times in your program you can start to see a difference. The first declaration ensures that you only call methods on myList that are defined by the List interface (so no ArrayList specific methods). If you've programmed to the interface this way, later on you can decide that you really need

List myList = new TreeList();

and you only have to change your code in that one spot. You already know that the rest of your code doesn't do anything that will be broken by changing the implementation because you programmed to the interface.

 
Alexandru Dragoi
Ranch Hand
Posts: 49
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is a good explanation.

Note that if you do not publish the reference to the ArrayList object (i.e. you do not make the object available outside its current scope: method, object, class), you could reference the object directly with an ArrayList variable.
You can use ArrayList behaviour inside the current scope, without worrying about someone else seeing the real implementation outside the current scope.

Thus:
ArrayList myList = new ArrayList(); // this is bad

is not bad, it may be even necessary under some circumstances (described above).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!