Skip to content

Java: Choosing the right variable type Interfaces vs concrete type

Kristina edited this page Jan 4, 2018 · 1 revision

In Java you can set a variable type to an interface variable type like List or a concrete variable type like ArrayList. To read more about the differences between a concrete object and an interface check out this article here.

When declaring a variable you should try to use a interface variable instead of concrete variable to make the code more flexible using polymorphism. You can assign any class that implements the interface to a interface variable type. For example if you use the List interface you can assign any of the following variable types to it:

  • ArrayList
  • AttributeList
  • CopyOnWriteArrayList
  • LinkedList
  • RoleList
  • RoleUnresolvedList
  • Stack
  • Vector

However if you use the concrete variable ArrayList you can only assign a ArrayList variable to it.

So for example with ArrayList you can only do the following:

ArrayList<String> arrayListExample= new ArrayList();

But with a List object you can do the following:

List<String> arrayListExample = new ArrayList();
List<Object> attributeListExample = new AttributeList();
List<String> copyOnWriteArrayListExample = new CopyOnWriteArrayList();
List<String> linkedListExample = new LinkedList();
List<Object> roleListExample = new RoleList();
List<Object> roleUnresolvedListExample = new RoleUnresolvedList();
List<String> stackExample = new Stack();
List<String> vectorExample = new Vector();

The AbstractList and AbstractSequentialList classes also implement the the List interface but they cannot be set to a List object because they are abstract classes which cannot be instantiated. You will get a "Cannot instantiate the type AbstractSequentialList\AbstractList" error in eclipse if you write the following code:

List<String> abstractListExample = new AbstractList()
List<String> abstractSequentialListExample = new AbstractSequentialList();

You cannot create an instance of a List object because it's a interface. So the following code will produce a "Cannot instantiate the type List" error in eclipse:

  List<String> listExample = new List();

To figure out all the different variable types you can assign to a interface variable simply look up the variable on Oracle's Java site and all the values under the "All Known Implementing Classes" are possible variables you can assign to the interface variable type if they aren't abstract classes.

For example if you look at the Java docs page for the List interface you will notice all the classes above are listed under the "All Known Implementing Classes" section.

Also interfaces can inherit other interfaces so if you look at the List interface you will notice it inherits the Collection and Iterable interfaces.

The Collection interface can be assigned to any of the classes that implements the Collection interface plus any classes that implement any of those interfaces.

Here is a list of the interfaces that inherits the Collection interface:

  • BeanContext
  • BeanContextServices
  • BlockingDeque
  • BlockingQueue
  • Deque
  • List
  • NavigableSet
  • Queue
  • Set
  • SortedSet
  • TransferQueue

A Collection variable can be assigned anything that can be assigned to a List interface and anything that can be assigned to a Set interface making it a very flexible variable that can accept a large number of different variable types.

Clone this wiki locally