\$\begingroup\$
\$\endgroup\$
1
I am implementing an API that implements an interface, but I do not need to implement all methods.
In that case, is it better to just return null or throw an IllegalStateException because it if not implemented?
Thanks.
@Override
public ResponseEntity get(long id) {
// ... some meaningful code
return response;
}
@Override
public ResponseEntity post(Request request, long id) {
// ... some meaningful code
return response;
// Not needed to implement this
@Override
public ResponseEntity put(Request request, long id) {
return null;
// or
throw IllegalStateException("not implemented yet")
asked Oct 21, 2022 at 9:36
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
You can use UnsupportedOperationException.
Don't return null
since it won't notify you about unintended use of the method and will result in a much more annoying NullPointerException instead.
answered Oct 21, 2022 at 11:34
-
3\$\begingroup\$ While the title change was appropriate, the question is still off-topic due to missing code review context. \$\endgroup\$2022年10月21日 13:01:53 +00:00Commented Oct 21, 2022 at 13:01
lang-java
// some meaningful code
bits are a problem because you're not asking about those methods, only theput
one. \$\endgroup\$