1

In some code snippets I see LinkedList<E> queue = new LinkedList<>() whereas in others I see List<E> queue = new LinkedList<>(). What are the differences here and is one way preferred over the other? Thanks!

badAtHooks
  • 139
  • 1
  • 3
  • 10
  • 1
    General practice is to refer every object as Interfaces. ```List``` is an interface and this makes good coding practise so that tomorrow, any one can reinitialise it to new Implementation of ```List``` like ```ArrayList<>()``` and move ahead without breaking any of the existing code. You can read more about this - Interface Segregation Principle – papaya Dec 25 '19 at 07:02
  • Makes sense @SiddarthSreeni ! – badAtHooks Dec 25 '19 at 07:03
  • It also enable you to use polymorphism. The whole design patterns in java work on this principle (Code to interface rather than to implementation). Since you are working with interface rather than a particular implementation, It makes your code loosely coupled and hence easily testable. – Tarun Dec 25 '19 at 07:13

0 Answers0