Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I need serializable should i use concrete List (e.g. ArrayList) or (Serializable)List?

Tags:

java

We have a discussion in office and cannot understand which approach is better

I have a class (SomeClass) with some method which receives Serializable object. The signature is following:

public void someMethod(Serializable serializableObject){
    ...
}

And I need to call this method from another class, but I should provide it with some List as fact parameter. There are two different approaches

1. Serializable

private SomeClass someClass;

public void doSomething() {
    List<String> al = new ArrayList<String>();

    al.add("text");

    someClass.someMethod((Serializable)al);
}

2. ArrayList

private SomeClass someClass;

public void doSomething() {
    ArrayList<String> al = new ArrayList<String>();

    al.add("text");

    someClass.someMethod(al);
}
  1. Benefit of the first example is that it adheres to the java’s best practices which says: use interface instead of concrete realization for reference type and any programmer while reading that source will understand that we don't need special behavior of the ArrayList. And the only place we need it's serializable behavior we are adding this behavior by casting it to the Serializable interface. And programmer can simply change this current realization of the List to some other serializable realization, for example, LinkedList, without any side affect on this element because we use interface List as it`s reference type.

  2. Benefit of the second example is that we refer to ArrayList as to class which have not only List behavior but also Serializable behavior. So if someone looked at this code and tried to change ArrayList to List he would receive a compile time error which would reduce time for programmer to understand what is going on there

UPDATE: we can't change someMethod signature. It came from a third-party company and we use it not only for Serializable Lists but also for Strings, Integers and some other Serializable objects

like image 267
agav Avatar asked Jul 29 '11 12:07

agav


3 Answers

You should use an interface when all you need is the methods an interface provides. (this is most cases) However, if you need more than one interface, you can use generics, but the simplest approach is to use the concrete type.

like image 161
Peter Lawrey Avatar answered Oct 13 '22 08:10

Peter Lawrey


It's better to define ArrayList because this combines two interfaces - List + Serializable. You need both of them in one place.

It doesn't matter that much, but not that using interfaces should be applied more strictly for return types, and less strictly for local variables.

like image 32
Bozho Avatar answered Oct 13 '22 07:10

Bozho


I would change the signature of the someMethod so that it reflects what it requires from the invoker of the method:

public class SomeClass {

    public <T extends List<? extends Serializable> & Serializable> void someMethod(T t) {

    }

    public static void main(String[] args) {
        SomeClass test = new SomeClass();
        test.someMethod(new ArrayList<String>());   //Works
        test.someMethod(new ArrayList<Image>());   //Compile time error, Image is not Serializable
        List<String> l = null;
        test.someMethod(l); //Compile time error
    }
}

The signature of someMethod now says that you must invoke it with something that is a List, and that is Serializable, and contains elements that are Serializable

like image 42
Kaj Avatar answered Oct 13 '22 09:10

Kaj