| Author |
Why instantiate an ArrayList polymorphically ?
|
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 69
|
|
Hey Guys,
I a m trying to figure out why the book uses:
as opposed to:
To quote the book from page 567:
In practice, you'll typically want to instantiate an ArrayList polymorphically
Why?
|
OCPJP 6
|
 |
Shaily Alex
Greenhorn
Joined: Nov 29, 2010
Posts: 2
|
|
You should declare it as a List<String>, and initialize it as an ArrayList<String>.
List is an interface, and ArrayList is an implementing class. It's almost always preferable to code against the interface and not the implementation. This way, if you need to change the implementation later, it won't break consumers who code against the interface.
|
 |
Dan Drillich
Ranch Hand
Joined: Jul 09, 2001
Posts: 1061
|
|
Please have a look at Why should ArrayList be assigned polymorphically as a List?
Regards,
Dan
|
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
|
 |
Seetharaman Venkatasamy
Bartender
Joined: Jan 28, 2008
Posts: 4503
|
|
|
most of the people want to know specification, because it is short!
|
Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
|
 |
Dennis Deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 547
|
|
It was an interesting discussion, but didn't really answer the question. I found the top response to a similar question posed at StackOverflow to be well stated.
In my opinion the best reason to declare List instead of ArrayList is that if you ever change your mind about using ArrayList, you will have lots of painful code cleanup to do.
|
OCPJP 6
|
 |
Ram Narayan.M
Ranch Hand
Joined: Jul 11, 2010
Posts: 241
|
|
The need of using Interfaces is to generalize the Structure of methods used in concrete class which provides same set of services (or) methods to the client (but internal implementation differs)...
If no concrete class follow interfaces, each class will provide the methods to the client in different format to client program. Whenever client desires to change from one service/method provider to other, many changes will be there in code...
So, Interfaces comes to rescue which imposes all concrete classes to follow the same structure of methods, so it facilitates the client program to use only Interfaces instead of confused with lot of concrete classes and client can change only the assignment part of
concrete class instance to "Interface" type reference.(That is, Generalization become effective due to Polymorphism feature)
In this case, we use "ArrayList" concrete class directly...Even, Concrete classes can be hided... We can write a separate class which provides the Concrete class instance to be assigned to reference of "Interface" type based on some key input (Key input maybe integer, character or string)...
|
SCJP 6 [SCJP - Old is Gold]
|
 |
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 69
|
|
Thank you guys especially Dennis.
:-)
|
 |
 |
|
|
subject: Why instantiate an ArrayList polymorphically ?
|
|
|