• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple doubt..Please clarify

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a very simple doubt, Please somebosy clarify me in detail.
We usually declare like this,

a.List arrayList = new ArrayList();
and not

b.ArrayList arrayList = new ArrayList();
Why?

and what is the advantage of declaring List arrayList rather than ArrayList arrayList.
If we declare like a) arrayList will not get any properties of ArrayList, it will just have that of List Interface.

Can any body clarify?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the principles of good Object-oriented design is to design objects that tell the cleint only what it needs to know, and hide the implementation details as much as possible. This way, you are free to change an implementation without affecting the client.

To use your example, suppose you created a JavaBean with the following property and corresponding getter/setter:

ArrayList myList = new ArrayList();

Then your class becomes an instant hit in the developer community and thousands of users download your class and start using it. Now let's suppose you're looking at the class and decide that an ArrayList isn't really the best implementation for what you're doing and decide to change it to a LinkedList. You then publish a new version of the class and tell everyone what a great improvement it is. Everyone gets excited about the new improvements and downloads the code. What happens? All the client code they created that used your class breaks! Your status changes from hero to zero. Since you changed the implementation of that one property, the signature of the getter/setter method changes, and the old client code doesn't work any more.

Now suppose you had defined it like this:

List myList = new ArrayList();

What happens when you change the implmentation? Nothing! The client code works just the same as it always did. You remain a hero!

Here's another advantage. Suppose someone wants to extend your class, and do things a little differently. When they override your method, they are free to use any implementation of the List interface they want and are not stuck with ArrayList.
[ February 15, 2007: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you don't have a need to get at the individual elements of your list then you might be better off still using this declaration:

Collection data = new ArrayList();

- Brent
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic