13

Suppose I have two interfaces:

interface IOne {

    public void method();

}

and

interface ITwo {

    public void method();

}

A concrete class implements both of the interfaces:

public class A implements IOne, ITwo {

    public void method(){
       //some implementation
    }
}

My questions are:

  1. Does the single implementation of method() suffice for both interfaces IOne and ITwo?
  2. If the answer of 1 is yes, is there any way to get both the methods in a single class? In this case it is not necessary we have to implement both interfaces in a single class.
Razib
  • 10,965
  • 11
  • 53
  • 80

4 Answers4

5

If both methods have the same signature, as it is the case in your example, only one implementation is possible. In this case, there is no way to implement two versions of the method for the two interfaces. So yes, the example will suffice.

If the signature is the same for the two methods, but they have different return types, this will result in a compilation error.

If the two methods have different signatures, there can and have to be two different implementations.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
4

For 1, the answer is yes. It's enough to provide one implementation for the method in the class for both interfaces to be automatically implemented.

For 2, if you need to have both methods, then your class should not implement both interfaces. However, there's a trick you can use:

interface IOne {

    void method();
}

interface ITwo {

    void method();
}

public class A
    implements IOne, ITwo {

    // Anonymous inner class that implements only IOne
    private final IOne one = new IOne() {

        @Override
        public void method() {
            System.out.println("IOne");
        }
    };

    // Anonymous inner class that implements only ITwo
    private final ITwo two = new ITwo() {

        @Override
        public void method() {
            System.out.println("ITwo");
        }
    };

    @Override
    public void method() {
        System.out.println("A");
    }

    // Method that delegates to IOne
    public void methodIOne() {
        this.one.method();
    }

    // Method that delegates to ITwo
    public void methodITwo() {
        this.two.method();
    }
}

Testing code:

A a = new A();
a.method(); // A
a.methodIOne(); // IOne
a.methodITwo(); // ITwo

Class A doesn't need to implement both interfaces. In that case, just don't implement method() on A and keep only the anonymous inner classes.

fps
  • 33,623
  • 8
  • 55
  • 110
  • What is the advantage of anonymous classes? That logic can be directly in `methodIOne()` and `methodITwo()`. – jaco0646 Jul 28 '15 at 17:59
  • 1
    @jaco0646 this is just a trick to have two different implementations of the same method. Yes, the logic could be there in those methods, but you wouldn't be enforcing interfaces contarcts. By delegating to inner classes and letting them implement the interfaces, you stick to what the method is supposed to do. Anyways this is a trick. Maybe the design is not that good if you have to do these things. – fps Jul 28 '15 at 18:07
  • 2
    This trick doesn't make much sense. If anything you'd want to pass in 2 instances of IOne and ITwo, then allow access to them with specific methods. Though this isn't a criticism of the answer, more of the question. I'm thinking the OP's design has some flaw. – William Morrison Jul 28 '15 at 18:27
  • @WilliamMorrison I agree. Actually, it doesn't make much sense to make a class implement 2 interfaces with the same method and have 2 different implementations for that method, one for each interface. But that's what the OP appeared to be needing. – fps Jul 28 '15 at 18:44
3

Does the single implementation of method() one suffice for both interface IOne and ITwo?

Yes. The implementation is the same for both abstract methods.

If the answer of 1 is yes, is there any way to get both the method in a single class?

No.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2
  1. Yes. That one method will be called when the class is accessed as an instance of IOne, or ITwo.

  2. No. A method's name, return type, and arguments define a method's signature, and two methods within the same class cannot have the same signature. Doing so results in a compile time error.

    One solution I suggest is to use different method names for IOne.method and ITwo.method, thereby making each method's signature unique. This would allow you to define two unique methods in a single class.

Community
  • 1
  • 1
William Morrison
  • 10,953
  • 2
  • 31
  • 48