0

I'm using Java as a sample. Let's say I'm developing a software and I have this

CustomList extends ArrayList<ObjectMapper>

Now this has custom functions added to ArrayList. It's designed specifically to cater to Lists of ObjectMapper instances (maybe JsonNode, TreeNode; belongs to jackson dependency). Now the entire project utilizes this. But someone mentioned this: "What if ArrayList gets deprecated or obsolete in the future?" If that does happen, we'd have to rewrite or modify the entire source code. So they called this Bad Design. How can I avoid this? Should I create an interface OurList that implements List and have it implemented as OurArrayList which will be what we will be utilizing? Please explain how it can be properly done. Say I want to be able to use ArrayList's iterate method. What's the best way to do so without compromising the project code maintainability?

Rei Brown
  • 185
  • 9
  • 4
    One solution would be to use "composition over inheritance". Why do you need all the methods of a ArrayList? I think you'd have bigger problems if ArrayList was actually deprecated – OneCricketeer Feb 02 '18 at 03:13
  • 1
    What if? What if Java gets deprecated in the future, then what? I'm joking of course, but deprecation isn't something you have to start worrying about with Java. There was a time when java.util.Vector was used in place of java.util.List. Sure it is no longer used, but to be deprecated, I think not! In terms of design though, it is a bad design to inherit in such a way. You may want to instead **use** array list inside your new class called `CustomList`. This is called delegation – smac89 Feb 02 '18 at 03:13
  • https://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition – shmosel Feb 02 '18 at 03:13
  • I made the question more specific on oop. It's not just about ArrayList – Rei Brown Feb 02 '18 at 03:18
  • Why you need custom implementation of List? – tsolakp Feb 02 '18 at 03:18
  • Because I want to add my own functions for that object. Not just specifically List. But in this case, List has a `sort()` function but I want to add a `sortMagicallyInTheMostAbsurdWay()` function. How would I do that? – Rei Brown Feb 02 '18 at 03:19
  • Then create a class that has `sortMagicallyInTheMostAbsurdWay()` (actually typed that out) and use `sortMagicallyInTheMostAbsurdWay()` (had to copy, this time) to sort a list inside your `CustomClass` – smac89 Feb 02 '18 at 03:21
  • If you composed a List/Collection, you can implement methods named whatever you want to do whatever you can to them. There's no overriding methods to worry about – OneCricketeer Feb 02 '18 at 03:21
  • @smac89 The reason, for example, that I'd want to extend `ArrayList` for that purpose is so I can do something like this `new CustomList().sortMagicallyInTheMostAbsurdWay()` – Rei Brown Feb 02 '18 at 03:23
  • @cricket_007 I'll try to look into composition for List/Collection. Thanks – Rei Brown Feb 02 '18 at 03:23
  • Then better option would be to define `sortMagicallyInTheMostAbsurdWay()` somewhere else like in utility class and have it take `List` as parameter. – tsolakp Feb 02 '18 at 03:25
  • Also see https://stackoverflow.com/questions/14974749/assigning-arraylist-to-list – OneCricketeer Feb 02 '18 at 03:28
  • I don't see what is wrong with `new CustomList().sortMagicallyInTheMostAbsurdWay()`. If the list is inside `CustomList`, then `sortMagicallyInTheMostAbsurdWay` will sort the list inside the class. Note if you are looking to access the private members of `ArrayList` by inheriting from it, you are going about it wrong. Child classes cannot access the parent's private members, only protected members can they access, and I don't think Java ArrayList exposes it's internals such as the underlying structure used to store the elements of the list, so looking to sort that is out of the question. – smac89 Feb 02 '18 at 03:28
  • Will expression like this work for you? `List l = Collections.customize(ArrayList::new).sortMagicallyInTheMostAbsurdWay().doSomethinElse().get()` – tsolakp Feb 02 '18 at 03:35
  • @smac89 Okay I'll look into that. Thanks – Rei Brown Feb 02 '18 at 03:48
  • Wait. @smac89 are you referring to composition? If I do composition, how will i still be able to utilize the other functions of List/ArrayList? Say I do make a CustomList class. I'll only be able to use the functions i create in that class, but I won't be able to use `new CustomList().add(Arrays.asList())` – Rei Brown Feb 02 '18 at 03:51
  • Sure you can. Declare a method called `add` which takes a list, and when called will add this list to the internal list inside your class. – smac89 Feb 02 '18 at 04:12
  • Even easier is to create an interface that declares what "`List`" methods your new class supports, then implement this interface – smac89 Feb 02 '18 at 04:13

2 Answers2

3

You are too worried, as ArrayList most likely won't be deprecated anytime soon.

Even if deprecated, don't think of it as a promise that your code will break.

You may have heard the term, "self-deprecating humor," or humor that minimizes the speaker's importance

A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it, since it has been superseded and may cease to exist in the future.

If your worries stem from obsoletion, you should focus more on requirements & meeting demand. Things become obsolete, it's inevitable, don't let it haunt you. ArrayList becoming deprecated shouldn't be a worry unless it's deprecated for a serious problem.

Do you worry about Thread being deprecated? Vector was quote popular until it was deprecated. You'll still see it around, as it wasn't remove to allow source backwards compatibility.

You'll be wasting time if you invest it in modifying code primarily to stay "up with the trends", rather than investing that time into current problems. Worrying about ArrayList being deprecated could be distracting you from other potential problems your project may have.

Vince
  • 14,470
  • 7
  • 39
  • 84
0

I'm almost certain ArrayList will never be deprecated. It's too widely used. However, if you're certain about doing that, use CustomList implements List