• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Make interface implementable by only 1 class

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
is this possible to make interface implementable by only 1 class? So if someone tries to implement it again it'll throw compilation error

Thanks.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why you have this requirement? Interface is designed for implementing.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Landon:
Hi,
is this possible to make interface implementable by only 1 class? So if someone tries to implement it again it'll throw compilation error

Thanks.



No. If you tell us why you think you need to do this, we may be able to suggest an alternative solution.
 
John Landon
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have a situation like this. I use jax-ws. There you should have endpoint interface. But the way my system is structured this interface will be in common code that is being accessed by different unrelated parts of the system. So I want each part to have only 1 implementation. Putting final classes in each module without the interface is bad too. Because it doesn't enforce the methods.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are writing all the implementation code yourself, then can't you just implement it only where you need it---even if that is a single class? You're not likely to implement it again yourself elsewhere are you? And in a proprietary app., no-one else will be writing code using your interface either. If you're working in a team, wouldn't a Javadoc comment in the interface description serve the same purpose, to alert your colleagues?

Edit: If you want to try to avoid the incident of someone in the team accidentally re-implementing the interface in a different part of the system, I don't see an easy way. A comment and explanation in the project documentation should do if you're working with a vigilant team.
[ October 28, 2008: Message edited by: Charles Lyons ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Landon:
I do have a situation like this. I use jax-ws. There you should have endpoint interface. But the way my system is structured this interface will be in common code that is being accessed by different unrelated parts of the system. So I want each part to have only 1 implementation. Putting final classes in each module without the interface is bad too. Because it doesn't enforce the methods.



What bad would happen if there was more than one implementation?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If at all you need an error to be thrown, I guess the JVM has to be tweaked a bit. Is nt it? Again, JVM comes into picture during the runtime.

We do NOT have any control with us because soon after you say 'implements XYZ' the control gets leveraged to JVM. If you need to have it tweaked at the compile time, I guess we may have to do something like a Plugin to the IDEs like eclipse where you would be developing!

Correct me if I am off the track!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would write a test for this rather than try to force the JVM into doing something it shouldn't. You could go through all classes tracking what they implement and see if more than one implement these "special" interfaces.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can think of a way to make it difficult to implement an interface, although not totally impossible. It involves mandating use of a specific IDE, so it's not very practical.

1) Declare your interface "I" in some package "p".

2) Include one method "m" in "I" which returns a package-protected type "T" -- i.e., a class which isn't visible outside of "p". "m" doesn't need to do anything useful.

3) Provide your one implementation inside "p".

So far, all this does is make it impossible to implement that interface without returning null from "m", since classes outside of "p" can't create instances of "T".

4) Use IntelliJ IDEA, which includes an enforceable @NotNull annotation, and apply @NotNull to method "m" in "I". The IDE would then try very hard to prevent anyone from returning null from an implementation of "m".

Now, this wouldn't be impossible to do, but it would be hard enough that the programmer would stop to wonder why the heck you made it so difficult!
[ November 01, 2008: Message edited by: Ernest Friedman-Hill ]
 
John Landon
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys.
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic