• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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 ?

Thanks
 
Sheriff
Posts: 3837
66
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic