31

Is it possible to make a child class that extends ArrayList? If so, how?

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
nambvarun
  • 1,201
  • 4
  • 13
  • 14

6 Answers6

22

You can extend any class that is not final in Java. Having said that, you should avoid inheritance if there is no true is-a relationship. Consider composition for reuse. Read about Liskov substitution principle

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
21

Yes you can.

public class MyArrayList<E> extends ArrayList<E>
{
}

However, I'm not sure why you would want to do this.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • 60
    to rename it something way more awesome than ArrayList, of course. -> List l = new MySuperAwesomeSandwichMakingConstantTimeLookupSwissArmyList(); – b3bop Feb 03 '12 at 03:07
  • 2
    Sorry for being super duper stupid, but what does the E mean? –  Sep 07 '17 at 07:28
  • 3
    @IchHabsDrauf When you create an ArrayList, you need to specify which type the list will hold. For example "new ArrayList()". Thus E is a placeholder for the type (in this case it is String). This called a "generic type". If you create an instance of MyArrayList you will do it like this: new MyArrayList(). This will pass the type "String" to the generic type of ArrayList and thus create a parent of ArrayList. – Armand Maree Sep 12 '17 at 01:08
10

As many other have said, yes, you can extend class ArrayList, but it is not something that you should normally do; it is not considered good practice in Java.

I'm mainly a Java programmer, but the past months I've also been working on C# code. It seems like it's a common idiom in C# to extend the standard collection classes if you need a collection of a specific type (I actually don't know if it is a common idiom in general - at least the people who wrote the code I'm working with are doing this all the time).

So if they have a class Person and they need a list of persons, they'd create a class PersonList that extends the C# equivalent of ArrayList<Person>.

The common idiom in Java would just to use ArrayList<Person> if you need a list of Person objects and not to create a specific subclass for this.

I'd advise you to stick to the common Java way of doing things, and not create your own subclasses of ArrayList or other collection classes.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Hey there. Since you're suggesting not to override the class, how would I go about getting a callback, when an item is added to a list? – Sipty Jan 22 '16 at 10:34
  • 2
    @Sipty For something like that, where you really want to add new functionality instead of just using it with a specific type argument (as in my description above), in my opinion it's legitimate to create a subclass. – Jesper Jan 22 '16 at 13:03
  • 1
    @Sipty Overriding is an option, but imagine your college adds a different independent functionality, so you have `ExtList1 extends ArrayList` and `ExtList2 extends ArrayList`. Now you want them both at once and you're out of luck. Or you need the same feature with a different base list implementation (maybe `LinkedList`, though it's virtually always wrong to use it). These are all cases when delegation wins. It needn't be more verbose when [someone has created the base already](https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/ForwardingList.html). – maaartinus Aug 18 '17 at 10:30
2

ArrayList is not final class and it provides public constructor, so technically it can be extended.

But best practice is delegate rather than extend. See: Decorator pattern

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • 8
    Ummm ... it is **not** best practice to use delegation instead of inheritance **in all situations**. You should make the decision on a case-by-case basis. – Stephen C Jan 24 '11 at 06:43
0

Just try it out. The class is not final, it's constructor is public, so you can. However, it's probably no good idea for a beginner.

Most of the time, it's no good idea for anyone. Imagine you add some functionality and get ExtList1 extends ArrayList. A college of yours adds a different independent functionality, so you have ExtList2 extends ArrayList. Now you want them both at once and you're out of luck.

Or you need the same feature with a different base list implementation (maybe LinkedList, though it's virtually always wrong to use it). Again, out of luck.

These are all cases when delegation wins. It needn't be more verbose when someone has created the base already.


I'd only inherit from ArrayList, if there was a very good reason for doing exactly this. Maybe some really extreme performance requirements based on proper JMH benchmarks.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
0

As others said, extending java lang data structures is a very bad idea. However, if you have some logic you want to isolate in a collection class, I would suggest bellow solution:

public class ProductCollection{

    ArrayList<Product> products;

    public Product getByCode(String code) {
       // ... your logic goes here.
    }
}
Ahmed Kooli
  • 713
  • 1
  • 6
  • 10