Why does the ArrayList in Java source code inherit from AbstractList and also implement the List interface?

by kpkmd54461 on 2012-02-15 22:10:35

Here is the translation of your question and the provided solutions into English:

---

**Why does the Java source code have `ArrayList` inherit from `AbstractList` and also implement the `List` interface, while `AbstractList` also implements the `List` interface? I hope for a detailed explanation.**

---

### **Solution 1:**

The `List` interface defines all the functionalities that any list must have.

`AbstractList` provides some common implementations.

---

### **Solution 2:**

The `ArrayList` class appears in two places:

1. In `@(#)ArrayList.java 1.50 05/09/02 java.util.ArrayList`:

```java

public class ArrayList extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable

```

2. In `@(#)Arrays.java 1.59 04/04/01` and `@(#)AbstractList.java 1.46 04/02/10 java.util.Arrays.ArrayList`:

```java

private static class ArrayList extends AbstractList

```

After analyzing `Arrays.java`, it seems like it was written first, followed by `java.util.ArrayList.java`. However, the specific reason is unclear. Later, it was noticed that the method `List subList(int fromIndex, int toIndex)` is not implemented. The reason for this is also unknown.

---

### **Solution 3:**

The `List` interface defines all the functionalities required for a list.

`AbstractList` provides some common implementations.

This explanation is entirely correct.

---

### **Solution 4:**

For further reference, you can check:

**"The lifecycle of elements in Java's ArrayList?"**

Link: [http://www.myexception.cn/j2me/875.html](http://www.myexception.cn/j2me/875.html)

Related topics:

- Modifying Android 2.2 Framework after changes causes "symbol not found" exceptions.

- Beginner question: Please guide on handling Chinese data read from MySQL appearing as garbled text in EXTJS textfield components.

---

### **Detailed Explanation:**

To address the original question more comprehensively:

1. **Purpose of Interfaces (`List`):**

- The `List` interface defines the contract for any class implementing it. It specifies methods such as `add()`, `get()`, `remove()`, etc., ensuring consistency across different list implementations.

2. **Role of Abstract Classes (`AbstractList`):**

- `AbstractList` provides a partial implementation of the `List` interface. It handles common functionality (e.g., size, iterator, etc.) so that concrete subclasses like `ArrayList` only need to implement a few core methods (e.g., `get()` and `size()`).

3. **Why Does `ArrayList` Extend `AbstractList`?**

- By extending `AbstractList`, `ArrayList` inherits the default implementations of many methods, reducing redundancy and simplifying its own implementation.

- For example, `AbstractList` already provides an implementation for the `iterator()` method, which `ArrayList` can reuse without rewriting it.

4. **Why Does `ArrayList` Also Implement `List`?**

- Even though `AbstractList` implements `List`, `ArrayList` explicitly declares that it implements `List` to clearly indicate its compliance with the `List` interface. This improves readability and ensures type safety.

5. **Unimplemented Methods (e.g., `subList`):**

- Some methods like `subList` are not directly implemented in `ArrayList` because they are inherited from `AbstractList`. However, these inherited implementations may not always be optimal for `ArrayList`. As a result, `ArrayList` overrides certain methods to provide more efficient or specialized behavior.

By following this design pattern, Java ensures flexibility, reusability, and maintainability in its collection framework.