This is not possible in Java, nor is it possible in any language (except the dynamics) I know. This is contrary to the idea of interface, because the concept of interfaces implies that a class that implements an interface ensures, as in a contract, the fulfillment of the interface as a whole and not partially. Failing to implement part of an interface is tantamount to breaking a contract! The "caller" of an interface need not worry whether the implementor of this one or that one method only and may assume that it "knows" to handle the entire interface.
However, since a Java class can implement multiple interfaces, I would segregate these interfaces into different interfaces and only implement those necessary for each case. This has several advantages, as it allows the code that "calls" the callback to inspect whether the passed class implements, or not, one of the interfaces, just by calling the implemented ones, and prevents you from having to implement all the methods in your class. Would do something like this:
public interface ICallbackSalvamentoUsuario {
void resultadoSalvar(boolean b);
}
public interface ICallbackResultadoUsuario {
void resultadoTrazer(Usuario u);
}
public interface ICallbackLeituraUsuario {
void resultadoListar(List<Usuario> lista);
}
public interface ICallbackResultadoExclusaoUsuario {
void resultadoExcluir(boolean b);
}
public interface ICallbackFalhaUsuario {
void falha(String f);
}
Thus, a class that allows saving, listing and the result of a user would look like this:
public class MinhaClasseUsuario implements
ICallbackSalvamentoUsuario,
ICallbackResultadoUsuario,
ICallbackLeituraUsuario {
public void resultadoSalvar(boolean b) {
}
public void resultadoTrazer(Usuario u) {
}
public void resultadoListar(List<Usuario> lista) {
}
}
Already an android activity that allows the deletion of users could be implemented so:
public class AtividadeExclusao extends AppCompatActivity
implements
ICallbackExclusaoUsuario,
ICallbackLeituraUsuario {
public void resultadoExcluir(boolean b) {
}
public void resultadoListar(List<Usuario> lista) {
}
}
Of course, the name of the interfaces and the methods you decide. As I do not know your architecture, I only used example names, the important thing is to understand the concept!
And you can also create interfaces that extend more than one of the above interfaces, allowing you to combine the above interfaces in groups that make it easy to define the interfaces that a class implements. Your initial Callbackuser would thus be defined:
public interface ICallbackUsuario extends
ICallbackSalvamentoUsuario,
ICallbackResultadoUsuario,
ICallbackLeituraUsuario,
ICallbackResultadoExclusaoUsuario,
ICallbackFalhaUsuario {
}
public class XYP implements ICallbackUsuario {
public void resultadoSalvar(boolean b) {
}
public void resultadoTrazer(Usuario u) {
}
public void resultadoListar(List<Usuario> lista) {
}
public void resultadoExcluir(boolean b) {
}
public void falha(String f) {
}
}