I always learn when we declare a collection we should do, Interface ob = new Class()
, if i want to use for example a LinkedList i'll do List ob = new LinkedList()
, but then i can't have access to all methods from LinkedList.. Isn't LinkedList ob = new LinkedList()
100% correct?

- 62,329
- 13
- 183
- 228

- 453
- 3
- 5
- 14
-
1LinkedList ob = new LinkedList(), creating object this way is just you want to use LinkedList class, nothing wrong with it.. Again it depends upon what you wanted from an implementation. – B. S. Rawat Mar 23 '12 at 18:15
-
1Depends what you want to achieve, check this out: [check your answer][1]here http://stackoverflow.com/questions/2451849/polymorphic-call – B. S. Rawat Mar 23 '12 at 18:13
-
1Good Q, Marcio. Just now, I came across a situation where I wanted to utilize `addFirst(E e)` and `addLast(E e)`, but could not find those in `List` interface. – Vikram Nov 16 '13 at 22:55
11 Answers
Isn't LinkedList ob = new LinkedList() 100% correct?
Well I'd suggest using the generic form, but sure - if you want to use functionality which is specific to LinkedList
, you need to declare the variable accordingly.
You might want to check whether the Deque<E>
or Queue<E>
interfaces have what you want though. If they do, use those in-keeping with the idea of describing what you need rather than what implementation you'll use.

- 1,421,763
- 867
- 9,128
- 9,194
-
3There aren't actually any methods _specific_ to `LinkedList`; everything either overrides a superclass method (like `clone()`) or implements a method of one of its interfaces. If you want to use methods specified by `Deque` (e.g., `peek()`) as well as methods specified by `List` (e.g., `indexOf()`), but not specified by both, then there's not much alternative to declaring the variable to be `LinkedList`. – Ted Hopp Mar 23 '12 at 18:07
Yes,
LinkedList<...> items = new LinkedList<...>();
is perfectly correct if you know that items
will depend on methods of LinkedList<T>
that are not captured in the List<T>
interface.

- 175,680
- 26
- 273
- 307
You should always try to keep the declaration at the highest level possible, meaning that you should stop at the highest level that provides all the functionality that you need: if List
methods are not enough, you're perfectly fine with your LinkedList
declaration.

- 1,825
- 2
- 20
- 26
The rule "always code to interfaces" must be taken with some flexibility. What you are suggesting is fine, and as you came to the conclusion, the only option.
As a side note, coding to concrete classes like this is faster is most JVMs. Deciding whether the performance is worth breaking the rule is the hard thing to decide.

- 6,405
- 6
- 28
- 69

- 22,176
- 9
- 79
- 134
-
2Do you have some data to back this claim? It's first time that I hear that coding to interfaces might be slower than to concrete classes. – Krzysztof Krasoń Mar 23 '12 at 18:05
-
@krzyk - It isn't too hard to test this yourself. Method calls to something like `list.get()` are measurably slower (if you do enough of them) when `list` is declared `List
` than when it is declared `ArrayList – Ted Hopp Mar 23 '12 at 18:44`. (For testing `get()`, I wouldn't use a `LinkedList`.) In a simple test I ran under Java 6, which just accessed every array element using `get()`, the generic calls took about 10% more for 1,000 elements, and about 18% more for 10,000 elements. -
http://developer.android.com/guide/practices/design/performance.html#myths. to be fair, this claim is concerning JVMs without a JIT. – Jeffrey Blattman Mar 23 '12 at 18:57
-
here's a SO post that talks more about it, http://stackoverflow.com/questions/6839943/why-are-interface-method-invocations-slower-than-concrete-invocations – Jeffrey Blattman Mar 23 '12 at 18:58
If you actually have a need to use methods that are not on the List interface, there is certainly nothing wrong with using LinkedList's API. The general rule of programming to the List interface recognizes that 1) it's pretty rare to need those methods, and 2) in most people's experience, it's way more likely that I discover I need to sort the list and/or use a lot of random access, and decide to switch to an ArrayList, than it is I need one of the methods only LinkedList has.
It may be also that you could be programming to the Queue interface, if you find List isn't giving you what you need.

- 47,174
- 11
- 83
- 83
LinkedList is a generic. You should be doing:
LinkedList<String> linkedList = new LinkedList<String>();
(or whatever else you need to store in there instead of String)

- 279
- 2
- 5
Nope.. This would be wrong, at the later stages if he wants to change his implementation from linked list to any other implementation of list type he will go wrong... So better to use the interface level declaration.

- 2,164
- 3
- 27
- 45
Not exactly 100% correct.
A preferred way to declare any collection is to include the data type it's holding. So, for your example, it'd be LinkedList<Integer> ob = new LinkedList<Integer>();
.

- 104,088
- 27
- 192
- 230
I won't always suggest you to use generics ..... Coz sometimes you may need to wrap different objects as here....
String str="a string";
boolean status=false;
LinkedList ll = new LinkedList();
ll.add(str);
ll.add(status);
In some situations like case of RMI, u can only send serialized data.....and suppose you want to send a class object(which is unserialized).......There you can wrap the members of the class(primitives) in a LinkedList and pass that object as a whole.......not worrying about the huge number of arguments...... Consider for eg:
public Class DataHouse
{
public int a;
public String str;
.
.
.
}
Now Somewhere u need to pass the objects.... You can do the following....
DataHouse dh =new DataHouse();
LinkedList ll = new LinkedList();
ll.add(dh.a);
ll.add(dh.str);
// Now the content is serialized and can pass it as a capsuled data......

- 2,081
- 4
- 20
- 33
you can still have access to LinkedList methods by using List, all you have to do is to type cast for example
((LinkedList)ob).add()
The point of using generic List and not LinkedList is because in case you simply change the type of lists you are using (let's say double linked list) your program will still work Generics are to simplify your code to be more portable and more "changeable"

- 4,093
- 6
- 52
- 88
Actually it would be better if it would be parametrized as both are raw types.

- 1,619
- 6
- 19
- 30