Why doesn't java.util.List implement Serializable?

34,846

Solution 1

List does not implement Serializable because is it not a key requirement for a list. There is no guarantee (or need) that every possible implementation of a List can be serialized.

LinkedList and ArrayList choose to do so, but that is specific to their implementation. Other List implementations may not be Serializable.

Solution 2

List is an interface and making it extend Serializable would mean that any implementation of List should be serializable.

The serializable property is not part of the List abstraction and should therefore not be required for an implementation.

Solution 3

No. A LinkedList is always a List. When you deserialize the linked list, since a LinkedList is a List, you may write

List l = (List) objectInputStream.readObject();

The fact that l is in fact a LinkedList is not important. You wanted a List, and you got a List.

Solution 4

Because List is open to be implemented by user specific subclasses as well, and implementors may not necessarily want to implement Serializable. Serializability does not belong to the key responsibilities of a List either, so there is no reason to link the two together.

Solution 5

Consider hypothetical ThreadList implements List<Thread>, containing the list of active threads at any given point in time. The implementation transparently browses active threads and allows easy access to them - for your convenience. Should such an implementation be serializable (forgetting that Thread is not serializable)?

It is up to the person implementing the interface to decide, whether her implementation is safe to be serialized. List is too generic, as basically stating *ordered collection of items of type T`.

Share:
34,846
Swaranga Sarma
Author by

Swaranga Sarma

Software Developer in Amazon.

Updated on February 01, 2020

Comments

  • Swaranga Sarma
    Swaranga Sarma over 4 years

    Why is it that java.util.List does not implement Serializable while subclasses like LinkedList, Arraylist do? Does not it seem to be against inheritance principles? For example if we want to send a Linkedlist over a network, we have to write:

    new ObjectOutputStream(some inputStream).writeObject(some LinkedList);
    

    So far so good, but while reading the object on the other side we have to explicity say LinkedList l = (LinkedList)objectInputStream.readObject(); instead of List l = (List)objectInputStream.readObject();. If we were ever to change the writing functionality from LinkedList to say ArrayList, we will also have to change the reading part. Having List implement Serializable would have solved the problem.

  • Swaranga Sarma
    Swaranga Sarma over 13 years
    In that case the same argument can be applied to LinkedList as well that being a very basic list, it does not to be serializable as it is not a requirement of a list.
  • JB Nizet
    JB Nizet over 13 years
    There is a difference in forcing all the implementations of a list to be serializable, and choosing that one particular implementation is serializable.
  • Dunaril
    Dunaril over 13 years
    LinkedList is an implementation, to which developers choose to give the serializable property. If we follow your reasoning no class should implement two interfaces at a time...
  • davin
    davin over 13 years
    presumably the OP meant that List should extend Serializable in addition to Collection, the fact that he used the word implement should be understood in context...
  • Dunaril
    Dunaril over 13 years
    Are you sure ? How would the runtime understand how to deserialize the object if you cast it to a non serializable interface ?
  • Puce
    Puce over 13 years
    The cast happens after the deserialization and is thus not important here -> cast to List is fine
  • JB Nizet
    JB Nizet about 13 years
    The runtime knows how to seserialize the object because its serialized byte stream contains the name of the class of the object that has been serialized. ObjectInputStream thus creates an instance of this class and populates it with the data found in the serialized object, and then returns it to the caller. The caller knows that it was a list that was serialized, and thus casts the returned object into a List.