Class A can implement multiple interfaces.
What I do not understand is what happens in this case:
public interface example1 {
void checkInt(int a);
}
public interface example2 {
void checkInt(int b);
}
public class class1 implements example1,example2 {
void checkInt(int c){
System.out.print(c);
}
this.checkint(5);
}
I tried to run it, and it gives me compilation errors. But my question is in general, can I implement two interfaces, that have a functions with the same signature?
What compilations errors are you getting? Because
this.checkintis in the wrong case, it should becheckInt(5)? Also you’re reducing the scope of the method from implied public (as all interface methods are) to default (packaged protected). ThecheckIntmethod needs to be public in the implementing class.Both interfaces define the method, but this is fine as there is one implementation on
class1. Soclass1fulfils the implemented methods of each interface with its implementation ofcheckInt(int i). With interfaces you’re only fulfilling a contract, so there is no ambiguity about which method to call – if you work with references of typeexample1orexample2it will still be the samecheckIntmethod onclass1which gets invoked.