Why collections need to be created this way:

List list = new Linkedlist(); 

Why is it not desirable to write LinkedList right away?

    4 answers 4

    This is an important technique for writing good OOP code. The idea is that your code becomes less dependent on the specific implementations of the modules used. You can write LinkedList , ArrayList , CopyOnWriteArrayList , etc. to the same List interface.

    At the same time, by announcing the list in this way, we can be sure that if it suddenly becomes necessary to change the implementation from LinkedList to ArrayList , then we will have to change only the line with the constructor call. For example, LinkedList , unlike ArrayList , in addition to the List interface implements another Queue (queue) in which there is a push method.

     List list = new LinkedList(); LinkedList linkedList = new LinkedList(); ArrayList arrayList = new ArrayList(); list.add("ok"); linkedList.add("ok"); arrayList.add("ok"); list.push("error"); //ошибка ΠΏΡ€ΠΈ компиляции, Ρƒ интСрфСйса List Π½Π΅Ρ‚ Ρ‚Π°ΠΊΠΎΠ³ΠΎ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°. linkedList.push("Ok"); arrayList.push("error"); //ошибка ΠΏΡ€ΠΈ компиляции, ArrayList Π½Π΅ ΠΈΠΌΠΏΠ»Π΅ΠΌΠ΅Π½Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Queue 

    In the example above, even though we have an instance of LinkedList in the list , we cannot call methods specific to it. The List interface forces us to use only list methods, not queues or anything else.

    Using an interface instead of an implementation is not so important when a collection (or other object) is created and used within one method, but it is important if it is somehow transferred to other modules.

    Read more about this topic about SOLID .

    • Thank you very much. Everything became clear. - Nojdon
    • This is important for parameter types, but not for local variables. What is the use of local variables? - Qwertiy ♦
    • @Qwertiy The code also loses functionality (LL methods for example) when writing List `a? And you can first write a List list = new LinckedList (); , and then when you need to redefine the methods from LL somehow? - Anton Sorokin
    • @ antonsorokin, yes, if he has special methods that are not included in the interface. - Qwertiy ♦

    List is an interface, and LinkedList is a class that implements this interface. interface List contains the basic methods add(), get(), remove() and so on, if you have enough of these methods you can write List<...> list = new LinkedList<..>() , if you need to use add. methods that exist in the LinkedList class should then be written as LinkedList<...> linkedList = new LinkedList<...>();

    • Maybe the other way around? LinkedList is a List interface? And it seems to me that there is clearly a reason why you need to write exactly as I wrote in the example. - Nojdon
    • @Nojdon, no, not the other way around. Cannot create interface instance. - Qwertiy ♦
    • @Qwertiy it seems to me that the TS meant why it is better to write List list = new Linkedlist(); not Linkedlist list = new Linkedlist(); - Alexey Shimansky
    • @Qwertiy, and where does a copy of the interface? I'm talking about the fact that list is the main interface, if more precisely, the collection. And it is from him that all the other collections are being implemented - Nojdon
    • 3
      @Nojdon, List is an interface, as I recall. LinkedList is a class that implements this interface. LinkedList cannot be an interface, because new Linkedlist() creates an instance, and an instance of the interface cannot be created. Therefore, the statement "LinkedList is a List interface" is incorrect. - Qwertiy ♦

    If you want to pull up from the base some entities that will be saved in your repositories as different sheets, you will have to check the model at least every time to see what kind of sheet there is. And by initializing through the interface you simplify your life for a few seconds. This is one of the examples, and there are many more such. For example, if you changed a sheet from array to linked in a model, but in services or controllers it pulled entities into an array, then you’ll have to manually change everything, but initiating this need from the interface disappears.

      Because this is the ideology of Java, declare variables with interface types.

      But that will change soon:
      https://habrahabr.ru/post/280188/
      https://habrahabr.ru/post/280075/

       val list = new Linkedlist(); 
      • This is the answer that suits me. Thanks for the specificity and separately for the links. - Nojdon
      • But I would like to know why)) After all, the question is this. Why they decided so, and not otherwise. Not a coin thrown. There is this some kind of well-thought-out explanation based on something - Aleksey Shimansky
      • @ Alexey Shimansky - Because the code should be written in the most general form. If the following code is sufficient for the List type, then the variable must be declared as List - as close to the base type as possible. - Igor
      • It would be nice to have a var, but also getters n setters like in c #. Can't hear anything about them? - Sergey
      • @Sergey, I'm not a javist. - Qwertiy ♦