Alex Rivera | Logout

Can you extend ArrayList in Java?

Asked 2011-01-24T06:18:00.620
31

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

Edit
Report

1 Answer

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.

answered 2011-01-24T06:21:06.640

Your Answer