Reputation: 5700
I have a interface:
public interface Service {
default Type service1(Type t) throws IOException {
throw new UnsupportedOperationException();
}
default Type service2(Type t) throws IOException {
throw new UnsupportedOperationException();
}
}
and a class implementing this interface but only needs service2:
class MyService implements Service{
@Override
Type service2(Type t) throws IOException {
...
}
}
Then I create a myservice-instance:
Service s = new MyService();
s.service1(t); // throws error at runtime
Is there a better way to hide or mark that service1() method to signal that nobody should use this unsupported method in code (for example, by annotation?). Or do I have to create another pattern for such cases?
Upvotes: 0
Views: 479
Reputation: 1762
The Service
interface (from your description of it) is a clear violation of the interface-segregation principle (ISP). You should have two interfaces for Service1
and Service2
. If you need an aggregation, you can get it through an interface called AggregatedService
which will extend Service1
and Service2
.
With your current design, it is not possible to hide one method only from the external system.
Upvotes: 4
Reputation: 119
An implementation inherits all methods from its implemented interface. So if you create an instance of this implementation, you can access all (non private) methods in this implementation and implemented interface.
Upvotes: 0
Reputation: 1303
Use the Deprecated
annotation. This tells other developers that this method is no longer supported, and planned to be removed in future versions. Your new interface might look like this:
public interface Service {
@Deprecated
default Type service1(Type t) throws IOException {
throw new UnsupportedOperationException();
}
default Type service2(Type t) throws IOException {
throw new UnsupportedOperationException();
}
}
Not only will it come up with a warning for people using it during compile-time, it will also highlight, telling people they are using deprecated Api's in most modern IDE's.
Upvotes: 0