Here’s one I’ve been scratching my head over for a while now.
Suppose you want to specify a method in an interface, but you don’t want it actually defined until the implementation level. That is to say, you want to specify a method in an interface such that if it is defined in an abstract class the compiler will throw an error.
public interface Thing{
public void method1();
public void method2();
//method3 should be implementation specific
public void method3();
}
And then I have an abstract class:
public abstract class BasicThing{
@Override
public void method1(){}
@Override
public void method2(){}
}
And my implementation specific code:
public class ThingImpl{
@Override
public void method3(){}
}
(By the way, I know that it’s an valid to just write it this way, but my reason in wanting to do this is so that if I’m writing the interface and passing it along to someone else who then passes it along to do the final implementation, I want to be able to force the extra specification on them)
No, there’s no way of doing this at compile-time. It’s a pretty odd requirement too, IMO.
You can do it the co-operation of the abstract class of course, as Sheik’s answer shows. But you can’t force an abstract class to do that by virtue of putting something specific in the interface.
You could potentially try to find all the relevant classes and validate your requirements in a unit test.