I read at many places that singletons can use interfaces. Some how I am unable to comprehend this.
Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement. This also means that a Singleton has at least 2 responsibities and this is not good OO design as classes should only have 1 responsibility and make sure they are good at that responisibility, but that is another discussion.
A singleton has an instance - it just never has more than one instance. You probably use a couple of static members for reference-fetching and to ensure that it never gets multiple instances, but for the most part the class is the same as any other class.
Something like:
public interface MyInterface
{
}
And
public class MySingleton implements MyInterface
{
private static MyInterface instance = new MySingleton();
private MySingleton()
{
}
public static MyInterface getInstance()
{
return instance;
}
}
Basically, a singleton class is a class which can be instantiated one and only once. The singleton class pattern is implemented by using a static method to get the instance of the singleton class and restricting access to its constructor(s).
As with the usage of an interface, it would be similar to the way any other class would implement an interface.
And also it shouldnt allow cloning of that object.