0

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();
    }
}
dunni
  • 43,386
  • 10
  • 104
  • 99
Ming Leung
  • 385
  • 2
  • 13
  • I think compiler will look for one overridden method in the Implementation class, even though you have 2 interface which contains the same method . So you can't differentiate. – Thanigai Arasu Jul 14 '17 at 10:28
  • 2
    Possible duplicate: https://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method – Pablo notPicasso Jul 14 '17 at 10:29
  • make two classes,and one implement one Interface,another implement another and make a main class that initiate both objects – Eliethesaiyan Jul 14 '17 at 10:29
  • [can-an-interface-extend-multiple-interfaces-in-java](https://stackoverflow.com/questions/19546357/can-an-interface-extend-multiple-interfaces-in-java) – Suresh Atta Jul 14 '17 at 10:38

2 Answers2

1

You can't

But to solve your problem you can do next.

  1. Remove implements BarObj.BarInt, FooObj.FooInt
  2. Remove method method
  3. Change trigger method

It 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.

talex
  • 17,973
  • 3
  • 29
  • 66
  • I forgot the name of my design pattern of using the interface in the class. – Ming Leung Jul 14 '17 at 21:38
  • Can you also discuss the pros and cons of both ways? I thought using the interface would be helpful for team work because we check the first line to know the members are using the right piece of code, and having the implemented method separately will make the file looks neat. But, if both interfaces use the same name (like my question), then the bug will be hard to find. – Ming Leung Jul 14 '17 at 21:44
  • @MingLeung My opinion that in your case the fact that you use those interfaces is implementation detail, so it shouldn't be part of class interface. Even if you had only one interface or different method names you shouldn't implement them in your class. This pattern occurs when people tried to save memory and not create extra instances. In modern day it is rarely the case. only if you create tons of instancies of this class. – talex Jul 17 '17 at 09:32
0

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.

Zameer
  • 1
  • 1