Reputation: 385
If two interfaces require to implement the method with the same name, then the method() is called twice. I need 2 methods implemented for 2 different interfaces, how can I implement both of them to do different things?
public class MainClass implements BarObj.BarInt, FooObj.FooInt{
MainClass(){
}
void trigger()
{
new BarObj(this);
new FooObj(this);
}
@Override
public void method() {
System.out.println("I DONT KNOW WHICH METHOD");
}
public static void main(String[] args) {
new MainClass().trigger();
}
}
public class BarObj {
interface BarInt
{
void method();
}
public BarObj(BarInt _barInt)
{
_barInt.method();
}
}
public class FooObj {
interface FooInt
{
public void method();
}
public FooObj(FooInt _fooInt)
{
_fooInt.method();
}
}
Upvotes: 0
Views: 105
Reputation: 20446
You can't
But to solve your problem you can do next.
implements BarObj.BarInt, FooObj.FooInt
method
methodtrigger
methodIt should look like this
void trigger()
{
new BarObj(new BarObj.BarInt(){
@Override
public void method() {
System.out.println("NOW I DO KNOW WHICH METHOD");
System.out.println("I'm bar");
}
});
new FooObj(new FooObj.FooInt(){
@Override
public void method() {
System.out.println("NOW I DO KNOW WHICH METHOD");
System.out.println("I'm foo");
}
});
}
It use anonymous class you can google for more detail.
Upvotes: 1
Reputation: 1
You can not have two methods with same name and same argument type and length in one class. When you implement two interfaces that have methods with same name, you need to give the implementation to only one method that acts as to be implemented for both interfaces because the methods in interfaces are empty and will never be executed. What will be executed is your class method that has been Overrided from the interfaces.
Upvotes: 0