-4

For example instead of:

LinkedStack<Integer> list = new LinkedStack<Integer>();

Should be this:

StackInterface<Integer> list = new LinkedStack<Integer>();
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

In my opinion:

LinkedStack<Integer> list = new LinkedStack<Integer>();

is better only if you need specific methods on LinkedStack that are not declared in StackInterface.

StackInterface<Integer> list = new LinkedStack<Integer>();

is better if you do not, because then you can 'plug in' any implementer of the interface for different reasons (efficiency reasons for example).

Patashu
  • 21,443
  • 3
  • 45
  • 53
  • I still don't find a good reason to use first option. If I have to use it by some means, I would prefer to encapsulate that portion of the code in a method with a comment warning that this method will try to use a specific implementation of the interface for a very specific job e.g. `TreeMap` or `TreeSet`. – Luiggi Mendoza Apr 16 '13 at 04:21
0

The second way is called code to interfaces.

Reference link

second way is prefarable like. use interface as refference for subclass object.

NPKR
  • 5,368
  • 4
  • 31
  • 48