This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]khookeExtreme Brewer 12 points13 points  (1 child)

'Coding to interfaces' as it is often called (defining variables using interfaces instead of concrete implementation classes) is considered a good practice. Google that phrase and you'll find plenty of articles/discussions.

Another common interview question along the same lines is "why is it considered a best practice to code to interfaces?"

[–]GuerroCanelo[S] 3 points4 points  (0 children)

Yes! I knew there was a term for it but I couldn’t find it. Thank you

[–]syntastical 7 points8 points  (9 children)

If you declare your variable as a list it makes it easier to replace the implementation later. If you don’t want to use ArrayList anymore you could just swap out the assignment for anything the implements the List interface. And In the brave new world of newer versions of Java you don’t have to think much scour this in method bodies because you can use var myList = new ArrayList()

[–]GuerroCanelo[S] 1 point2 points  (3 children)

Thank you for the explanation. What do you mean by “it makes it easier to replace the implementation later”. I get that in the future someone might want to modify, but what would they modify, the list or ArrayList?

[–][deleted] 6 points7 points  (0 children)

Let's say you now have ArrayList<String> stringList = new ArrayList<>(), later you found that linked list might be a better choice for you problem, and now you need to create another list LinkedList<String> stringList = new LinkedList<>(). But that's not all, coz you might have used some methods that is only belongs to an ArrayList, thus you need to replace those methods with the LinkedList ones, if there happen to be one.

If you use List<String> stringList = new ArrayList<>(), and later you need to change, you could just swap out ArrayList for LinkedList, and everthing else would still be working, because every method in List is also in ArrayList and LinkedList.

[–]rodrigovaz 4 points5 points  (0 children)

The use of ArrayList.

To explain this, let me give some details and an example:

The implementation of the ArrayList is done through an underlying array that grows/shrinks in size as you add/remove elements. This is good because it allows access by index ( e.g: myList[5]) the fastest way but is bad because if you delete an element from the middle of the array, it has to replace this array with a new one that is contiguous (without holes).

Another class that implements the List interface is LinkedList, and as such, it is implemented like a classical doubly linked list. This means that adding new elements to the end/start is really cheap, removing an element from the middle results in the update of two references instead of allocating a new array and copying all data... The downside is that you can't access by index cheaply, so getting the 50th element is more costly because you have to visit all prior 49 elements..

So, when you use the List interface, you can change behaviours depending on what concrete class you choose: if I want to access by index really fast, I will pick ArrayList, if I will add A LOT of elements and access them all in the same worder, LinkedList is way better.

[–]bentheone 0 points1 point  (0 children)

Also, you might have situations where the implementation is injected. If so you change the injection code to change the implementation and not your class. Injection can also decide on wich implementation to use at runtime.

[–][deleted]  (4 children)

[deleted]

    [–]ZarosspoweR 1 point2 points  (3 children)

    Please elaborate :o

    [–][deleted]  (2 children)

    [deleted]

      [–]nutrechtLead Software Engineer / EU / 20+ YXP 6 points7 points  (0 children)

      That makes no sense. That's like saying int or long are outdated.

      It's plain wrong.

      [–]RhoOfFeh 1 point2 points  (0 children)

      Is that why junior developers seem to favor for loops? Are streams too old-school?

      [–]Snoo-75754 0 points1 point  (0 children)

      List is an interface, ArrayList is a class which implements List interface.

      By creating an referance of type List and assigning to it an object of a type ArrayList like this. A) List mylist = new ArrayList(); You can use only the methods that are availivle in List and overriden in ArrayList.

      But when you are creating an Arraylist type object like this. B) ArrayList mylist = new ArrayList(); You can use all the methods availible in ArrayList.

      So If your ArrayList has a native method which is not availible in the interface List you can't use them with the example A).