Reputation: 8486
I am new to java and when I go through the code of many examples on net I see people declaring the variable for an ArrayList
as simply List
in almost all the cases.
List<String> myList = new ArrayList<String>();
I don't understand if there is some specific advantage of doing this. Why can't it be an ArrayList
itself, like so:
ArrayList<String> myList = new ArrayList<String>();
Upvotes: 10
Views: 6950
Reputation: 3738
1) List
is an interface, the implementation class is ArrayList
2) List
interface extends Collection
.
Upvotes: 0
Reputation: 1616
List is an interface so it can store referance to all classes extending from List Interface.If you used following statment then after some programing steps you can also store other Objects(extending from List) referance.
List<String> myList = new ArrayList<String>();
...
myList =new LinkedList<String>();
The main advantage comes with this is code maintenance. For instance, if someone comes and wants to use a LinkedList instead of the ArrayList, you only need to change one line rather than zillions throughout the code. It makes your code a little more flexible.
The whole concept of polymorphism in java relies on java's ability to use interface/base class reference to reference to sub class objects.
Also, programming to an interface rather than to the actual implementation is always seen as a best practice. This would allow the developer to replace one implementation with other/better implementation easily when required.
Upvotes: 2
Reputation: 29503
Java is strongly typed object-oriented language. ....
Let's go through an example: Suppose that you are writing a sort function sort(ArrayList<> list)
. If the function declared in that way, java wont allow you to sort anything else with that function than ArrayList
. Better approach would be to define an abstraction (as in your question is) List
. In that abstraction you declare methods. that all implementations must implement-define (ie ArrayList). Now your sort
function can be declared as sort(List<> list)
and you can sort with it anything that implements List
(and follows the contract of the List
methods).
In this way you can reuse code that you wrote for all your implementations as long as they obey the contract of the abstraction. In Java you achieve this using interfaces as abstractions and classes as implementations. There are other advantages too, but you have read some book about Object-oriented programming if you want to understand them better.
Upvotes: 1
Reputation: 57222
This isn't a cast but rather you are using the interface as the declared type of the variable. The reason for doing this is that you can switch which implementation of List you are using. If users of the list only depend on the List interface, they won't be affected if you using another list type, such as LinkedList.
Upvotes: 2
Reputation: 54312
Because if you decide to change it later it's easier:
List<String> myList = new ArrayList<String>();
List<String> myList = new LinkedList<String>();
It's more important in method signatures, when changing what you return can force everyone using your function to change their code.
Upvotes: 4
Reputation: 44808
It's called programming to an interface. It allows you to substitute that ArrayList
for a LinkedList
if somewhere down the line you decide that a LinkedList
would be more appropriate.
Upvotes: 15