Re: List Interfaces
Alper skrev:
Hello people,
I start programming with java 2 months ago and it goes pretty well for
now. I was exploring the Collections Interface and I have a question
about it.
Why would u use this line;
List list = new ArrayList();
Instead of this one;
ArrayList alist = new ArrayList();
Aside from the fine answers you've received, there's little reason to
choose your first example over your second in a method body, for
example:
private void test() {
List customers = new ArrayList();
customers = addPaymentOutstandingCustomers();
sendLetterTo(customers);
}
It doesn't matter whether you use an ArrayList or a List in that
declaration, because if you change the ArryayList to a Vector, then the
following two lines don't change.
Where the principle of, "Program to an interface, not an
implementation," carries more weight in Java is in method declarations.
For example, if the sendLetterTo() method above should be declared as,
public void sendLetterTo(Collection collection);
So that the implementation of the collection can change without
affecting code. But it doesn't matter if, in the method body shown
above - and for this example - "customers" was declared as ArrayList or
List.
..ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition