0
public interface Cache<T>{
    public void put(String key,T value);
    public Object get(Object key);
}

public class CacheImpl<T> implements Cache {
    private static Object monitor = new Object();
    private static CacheImpl instance;
    private Map<String, T> cache = Collections.synchronizedMap(new HashMap<String, T>());

}

In the above code I have to use generics for Type Safety Check, but getting error in implemented class.

Paul Bellora
  • 51,514
  • 17
  • 127
  • 176
user1347618
  • 49
  • 3
  • 11

2 Answers2

3

Your implementation class has not implemented any of the declared methods in the interface!

An interface is a contract whereby the implementing class promises to support the methods in the interface. You need to add implementations for the methods declared in the interface.

You also need to include the generic type argument in your implements clause:

public class CacheImpl<T> implements Cache<T> {

    public void put(String key,T value) {
      // Method body
    }

    public Object get(Object key){
      // Method body

      return someObject;
    }

    // Other methods
}
Peter Gluck
  • 7,914
  • 1
  • 36
  • 35
  • but inside my getInstance method for singleton mechanism i am not able to use generics. public static CacheImpl> getInstance() { if (instance == null) { synchronized (monitor) { if (instance == null) { instance = new CacheImpl>(); } } } return instance; } – user1347618 Apr 24 '13 at 00:16
  • I wanted to use singleton also along with it. – user1347618 Apr 24 '13 at 00:18
  • Why can you not use generics? It would be helpful if you told us what language and error message. At any rate, your code will not compile until you implement the interface. – Peter Gluck Apr 24 '13 at 00:34
  • Language is JAVA, If i use generics like CacheImpl and try to instantiate this in static getInstanceMethod() I get error message: "Cannot make a static reference to the non-static type T" – user1347618 Apr 24 '13 at 00:41
  • public static CacheImpl getInstance() { if (instance == null) { synchronized (monitor) { if (instance == null) { instance = new CacheImpl(); } } } return instance; } – user1347618 Apr 24 '13 at 00:42
  • instead of above code if I use below code its working fine, any issue with below code ? private static final CacheImpl> instance1 = new CacheImpl(); public T variable; public static CacheImpl> getInstance1() { return instance1; } – user1347618 Apr 24 '13 at 00:47
  • I would avoid Singletons. They are some kind of bad practice. http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons Use Dependency-Injection instead. – Christian Kuetbach Apr 24 '13 at 20:34
0

Singleton with double check and generics paramenter can be handled see below, where CacheImpl is my Cache implementation

public static CacheImpl<?> getInstance(Class<?> ofClass) {
        if (instance == null) {
            synchronized (CacheImpl.class) {
                instance = new CacheImpl<Object>(ofClass);
            }
        }
        return instance;
    }
user1347618
  • 49
  • 3
  • 11
  • Singletons are some kind of bad practice. http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons Use Dependency-Injection instead. – Christian Kuetbach Apr 24 '13 at 20:34
  • Shouldn't you check for instance being null in the synchronized context again? Otherwise it is pretty pointless because you can end up in the if with two threads and they would both instantiate the CacheImpl. So, you'd either have to wrap the whole null check into the synchronized or if you don't want the overhead of the synchronized in every call, you'd have to check again within the synchronized. – Stefan Fabian Oct 24 '17 at 21:08