Search code examples
javacollectionsserializable

Can any class that implements java.io.serializable be added to an array list of generic type <Serializable>


Description:

I have an ArrayList that takes any class which implements Serializable. I am even able to add instances of my classes that implement Serializable to this array list without any compilation errors. How does Java Generics handle adding these serializable objects to this List?

I am aware of the fact that we cannot define a reference List<Object> l

However I don't see any compilation problems with my List<Serializable> serializables reference.

My second question I have here is how do I retrieve my elements in these objects that are added to this serializable list?

If an answer has already been posted for a similar question, please post your links.

/***Here is my main class***/

import java.io.Serializable;
import java.util.*;

public class LearnArrayList {
    public static void main(String[] args) {

        List<Serializable> serializables = new ArrayList<Serializable>();
        serializables.add(new RandomClass1());
        serializables.add(new RandomClass2());

    }
}

/***Here is my RandomClass1 that I will add it to List<Serializable>***/

import java.io.Serializable;

public class RandomClass1 implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int a;
    private int b;

/*Getters and setters here*/
}

/***Here is my RandomClass2 that I will add it to List<Serializable>***/
import java.io.Serializable;

public class RandomClass2 implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String s;

/*Getters and setters here*/
}

Solution

  • Please refer to below code snippet. I hope this is self explanatory.

        public static void main(String[] args) {
        List<Serializable> serializables = new ArrayList<Serializable>();
        serializables.add(new RandomClass1());
        serializables.add(new RandomClass2());
        process(serializables);
        process2(serializables);//no issues
    
        List<RandomClass1> randomClassList = new ArrayList<RandomClass1>();
        randomClassList.add(new RandomClass1());
        randomClassList.add(new RandomClass1());
        process(randomClassList);
        process2(randomClassList);//compile time error in this line
    }
    
    
    private static void process(List<? extends Serializable> list){
        Iterator<? extends Serializable> itr = list.iterator();
        while (itr.hasNext()){
            Serializable s = itr.next();
            System.out.println("process1: " + s.getClass());
        }
    }    
    
    private static void process2(List<Serializable> list){
        Iterator<Serializable> itr = list.iterator();
        while (itr.hasNext()){
            Serializable s = itr.next();
            System.out.println("process2: " + s.getClass());
        }
    }
    

    Output is

    process1: RandomClass1

    process1: RandomClass2

    process2: RandomClass1

    process2: RandomClass2

    process1: RandomClass1

    process1: RandomClass1