• Post Reply Bookmark Topic Watch Topic
  • New Topic

Make interface implementable by only 1 class  RSS feed

 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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.
 
Haulyn Jason
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why you have this requirement? Interface is designed for implementing.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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: 239
  • Mark post as helpful
  • send pies
  • 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.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!