This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to make implementation of an Interface must

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • 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: 3752
62
Netbeans IDE Oracle Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!