If I understand your question, you're asking how to back the interface with an existing java class like HashMap.
You will have to create your own implementation that implements your interface, and extends HashMap.
The Interface methods will delegate the action to the HashMap. In this manner, your custom class will meet the interface needs for any Method with a signature of your interface.
A working example I threw together to showcase how to do what you're asking with Generics:
The Cache Interface:
package interfaces;
public interface Cache<K,V> {
V get(K key);
V put(K key, V value);
void clear();
}
The Implementation:
package classes;
import java.util.HashMap;
import interfaces.Cache;
public class MyCache<K,V> extends HashMap<K,V> implements Cache<K,V> {
private static final long serialVersionUID = 1L;
@Override
public V get(Object key) {
return super.get(key);
}
@Override
public V put(K key, V value) {
return super.put(key, value);
}
@Override
public void clear() {
super.clear();
}
}
You can then use it like so:
import classes.MyCache;
import interfaces.Cache;
public class DoStuff {
private Cache<String,Integer> cache;
public void initCache() {
//This works
//Since MyCache implements Cache
MyCache<String,Integer> mCache=new MyCache<String,Integer>();
setCache(mCache);
//This will not
//Since HashMap does not implement Cache
HashMap<String,Integer> mMap=new HashMap<String,Integer>();
setCache(mMap);
}
public void setCache(Cache<String,Integer> c) {
cache=c;
}
public Cache<String,Integer> getCache() {
return cache;
}
}