If I want to make sure that to obtain some behavior, an Interface must be implemented, how can I do that ?
e.g. Serializable must be implemented for a class to be Serialized. How does JVM understand this ?
If I create an interface and want that it must be implemented, what do I need to do ? Do I need to make modification to JVM ?
The natural way to make someone provide an implementation of an interface is to have a method which takes that interface as a parameter; for example:
Someone wanting to call the move method will have to provide an instance of the Movable interface, possibly by implementing one's own.
The Serializable interface is a bit different, however, and it is probably not a good thing to imitate. Essentially it's a marker interface (with some black magic added which I'M not going to go into here). Marker interfaces are used to mark classes that are special in some aspects which aren't manifested by the methods of that class. A better example of this might be the RandomAccess marker interface. This is an interface to be used (implemented) by implementations of the List interface to indicate the List implementation can efficiently access its members in random order. An ArrayList, for example, can. A LinkedList, on the other hand, cannot. Some algorithms using Lists (such as sorting) are more efficient if they can access the list in random order. The code could then look something like this:
This is a bad design, in my opinion. Much better would be to add a method to the List interface itself which would provide the same information:
The sort method would then look like this:
This would be much better. Someone wanting to create a new List implementation would have to implement the isRandomAccess method. The way it is done now, if you created an ArrayList from scratch (for whatever reason), you might forget to implement the RandomAccess interface even though the implementation itself does access items in random order efficiently. Especially as the JavaDoc of the List interface itself doesn't mention RandomAccess at all.
Moreover, since Java 6 or so, if you feel like creating a marker interface, you should probably create an annotation instead.