Jump to content
  • Advertisement
Sign in to follow this  
Luctus

[java] Optional methods in interfaces

This topic is 5125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Looking at the interface for Iterator (link) in the javadoc, one can see that the remove method is optional to implement. How do one declare an interface method to be optional?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You can't. It's downright evil to put an interface invariant like that in human-readable non-compilable documentation.

Which is one of the reasons why it sucks that Sun won't put some time/money/effort into adding proper design-by-contract (of any form) into java.

I wouldn't worry about it much though - people do it every now and then in libraries usually because they're too damn lazy to make two separate interfaces (which is, to be honest, what Sun should have done here!). As long as it happens rarely it's tolerable, although mildly irritating.

redmilamber

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Tired. I meant "to put an exception to an interface requirement".

The docs are attempting to break the compile-time requirement. You get away with compiling this by implementing the method:

remove()
{
throw new UnsupportedOperationException( "Not implemented. Go away" );
}

UOE is an excellent exception, because you can put it into code as you're writing it to indicate "Not implemented (yet)". But IMHO Sun are really stretching the bounds of acceptability by putting it one of the most important of the core libraries!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!