Reputation: 15553
When using collections in Java, we are advised to use Interface instead of concrete types.
Like: List<Object> list = new ArrayList<Object>();
But, using ArrayList<Object> list = new ArrayList<Object>();
will also does the same job, right?
Upvotes: 2
Views: 3729
Reputation: 23
Say you have a class with the following method
public ArrayList<T> foo (ArrayList<T> someInput) {
//Do some operations on someInput here...
return someOutput;
}
Now, what happens if you change the program so that it uses LinkedList
objects instead of ArrayList
objects? You will get a compiler error wherever this method is called, and you would have to go through and refactor your code so that it accepts LinkedList
objects.
If you had programmed to an interface and used a List instead:
public List<T> foo (List<T> someInput) {
//Do some operations on someInput here....
return someOutput;
}
If this was the case, no refactoring would be necessary as both the LinkedList
and ArrayList
classes implement List
so there would be no compiler errors. This makes it incredibly flexible. It does not matter to the method what it takes in and what it returns, as long as the objects implement the List interface. This allows you to define behaviour without exposing any of the underlying implementation.
Upvotes: 1
Reputation: 17014
There's a useful principle: for declared types, use the loosest (vaguest) interface possible (and List
is 'looser' than ArrayList
).
In practice, this means if you only need to access methods declared in List<Object>
on your list instance (which is actually an ArrayList
), then declare it as List<Object>
. This means you can change your mind on the exact type of list later and you only need to change the line that actually instantiates the ArrayList
(or LinkedList
or whatever you choose).
This has implications for method signature too: if you were passing around an ArrayList
instead of a List
, and then changed your mind about it being an ArrayList
, you have to go and edit lots of method signatures.
Please read up on Polymorphism if you'd like to know more.
Tangentially related is the Liskov Substitution Principle:
What is the Liskov Substitution Principle?
Upvotes: 2
Reputation: 5492
Interfaces or should I say base calsses are used to generalize things and problems at hand. So when you implement an interface you can always get the specific objects.
For example:
From Animal interface
or super class
you can always derive specific interfaces or calsses like Lion
, but not the other way, becaus its true that a Lion is an animal but several other animals cannot be derived from Lion. Thats why it is advised to make things general and hence use interfaces
.
Same applies in your case. You can always get ArrayList
and other implementations from a List
.
Upvotes: 1
Reputation: 121998
That is the Polymorphism which is the core concept of OOP.
It means ‘a state of having many shapes’ or ‘the capacity to take on different forms’. When applied to OOP , it describes a language’s ability to process objects of various types and classes through a single, uniform interface.
List
is a Uniform interface and its Different implementations are like ArrayList ,LinkedList
.....etc
Prefer to read :What does it mean to program to a interface?
Upvotes: 4
Reputation: 43738
Yes, but if you later change your mind and use a LinkedList
You have to change much more in your code.
Upvotes: 8
Reputation: 13844
When you define your list as:
List myList = new ArrayList();
you can only call methods and reference members that belong to List class. If you define it as:
ArrayList myList = new ArrayList();
you'll be able to invoke ArrayList specific methods and use ArrayList specific members in addition to those inherited from List.
Nevertheless, when you call a method of a List class in the first example, which was overridden in ArrayList
, then method from ArrayList
will be called not the one in the List.
Also the first has the advantage that the implementation of the List can change (to a LinkedList for example), without affecting the rest of the code. This is will be difficult to do with an ArrayList
, not only because you will need to change ArrayList
to LinkedList
everywhere, but also because you may have used ArrayList
specific methods.
Upvotes: 2