Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   5. Best practice tips  >   5.2. Interface type variables

5.2. Interface type variables
Last updated: 1 February 2013.

This page will show you the benefits of using interfaces as variable types.

When you need to declare a variable whose type is a class that implements an interface, it is wise to give to the variable the interface type. For example, the following statement declares a variable of type Map that will be instantiated as a HashMap at runtime:

Map<String, Object> map = new HashMap<String, Object>();

The above statement is correct. You can declare a variable as an interface and instantiate it as a class which implements the declared interface. In this example, you can instantiate the variable as a HashMap because the class HashMap implements the interface Map.

Since the type of the variable map is the interface Map, you can pass it to a method that takes a Map interface argument like the method dealWithMap shown below:

public void dealWithMap(Map<String, Object> arg){
   
// Do something with map
}

Now suppose that you decide to instantiate the variable map as a LinkedHashMap instead of a HashMap because you need its entries to be ordered. The new declaration would be the following:

Map<String, Object> map = new LinkedHashMap<String, Object>();

The benefit of such a kind of declaration is that you can still pass the variable map to the method dealWithMap, although its runtime type has changed. In fact, that flexibility can only be achieved if you declare method arguments as interfaces whenever possible, which is highly recommended. Additionally, if those methods have a return type, it should be an interface type. That way, changes in their implementation do not affect their signature. As an example, a method that returns a HashMap instance should have a Map return type as follows:

import java.util.HashMap;
import java.util.Map;

public final class MyClass {

   
public Map<String, Object> returnHashMapInstance(){
         
Map<String, Object> result = new HashMap<String, Object>();

         
// ...
          // ...

         
return result;
   
}

}

You can call the method returnHashMapInstance like this:

MyClass myClass = new MyClass();
Map<String, Object> map = myClass.returnHashMapInstance
();

If the implementation of the method returnHashMapInstance changes (let's say it returns a TreeMap instead of a HashMap), its signature (particularly its return type) will not change because the class TreeMap implements the interface Map as well.

In short, when it's possible, use interface types instead of concrete types when you declare variables and method arguments.

To take another example, the preferred way to declare an ArrayList variable and a Vector variable is the following:

List<String> list1 = new ArrayList<String>();
List<String> list2 =
new Vector<String>();


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   5. Best practice tips  >   5.2. Interface type variables
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   5. Best practice tips  >   5.3. Performance tips

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7