RSS Feed for This PostCurrent Article

Java: Always Refer to Objects By Interfaces

In whatever OO programming languages, you should always refer to objects by interfaces. E.g.

List<Msisdn> msisdnList = new ArrayList<Msisdn>();
List<Msisdn> msisdnList2 = new Vector<Msisdn>();
Map<String,String> msisdnLookup = new HashMap<String,String>();
Map<String,String> msisdnLookup2 = new Hashtable<String,String>();

By doing this, you program will be more flexible. If you decide that you want to switch implementations, all you have to do is change the class name in the constructor (or use a different static factory).

The exceptions are

  • If the implementation contains specific methods that you want to use
  • No interfaces available for that class.

Trackback URL

RSS Feed for This PostPost a Comment

Refresh Image