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();
    }
}
4
  • 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. Commented Jul 14, 2017 at 10:28
  • 2
    Possible duplicate: stackoverflow.com/questions/2801878/… Commented Jul 14, 2017 at 10:29
  • make two classes,and one implement one Interface,another implement another and make a main class that initiate both objects Commented Jul 14, 2017 at 10:29
  • can-an-interface-extend-multiple-interfaces-in-java Commented Jul 14, 2017 at 10:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I forgot the name of my design pattern of using the interface in the class.
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.
@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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.