• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to make implementation of an Interface must

Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

Posts: 3837
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic