4

Given to interfaces I1 and I2

interface I1{int i=1; void foo();}

interface I2{int i=2; int foo();}

is there any way for a class A to implements both of them? I can't manage to correctly implement the foo methods, and this makes sense, because having the same input parameters in I1.foo() and I2.foo() wouldn't let class A distinguish between the two.

But the constant "i" is not a problem, provided that you cast A to one of the interfaces when trying to read them:

System.out.println(((I2)new A()).i);

it looks like the compiler says: "Ok i'm leaving you with an ambiguity in 'potentiality', but when it comes to 'actuality' i'm going to stop you". And the "actuality" for the methods seems to come earlier.

Am I right? Am I missing something? Am i just going crazy imagining a compiler talking to me like an aristotelian?

1

3 Answers 3

4

No. You can think of interfaces in Java as contracts. Because these interfaces have the same method name (but different return types) no one class can fulfil both contracts. If they had different arguments the answer would be yes.

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

5 Comments

and what about field "i"? it compiles, but doesn't allow to create an instance of class A
@Ph.Voronov In an interface? It's not a field, it's a constant.
Yes, i'm about this constants. "interface I1{int i=1;} interface I2{int i=2;} class A implements I1, I2 {}" compiles, but a compile error occurred at attempt to instantiate A.
You don't usually access a constant on an instance anyway. Also, compiling is not a guarantee of correctness.
@Ph.Voronov it's not exactly true. You can instantiate A, the compile error occur when trying to read the value of "i" as member of "A" (from inside or outside A itself)
4

If you are using Java 8, you could do something like this:

interface I1 {
  default void foo() {
    //some default implementation
  }
}

interface I2 {
  default int foo() {
    //some default implementation
  }
}

public class SomeClass implements I1, I2 {
  I2.super.foo();
}

Comments

0

Trying to fulfil a multitude of interfaces, might be better solved by a lookup discovery. The largest advantage is decoupling of dependencies, and dynamic discovery.

(I am aware this does not target the theorising.)

class A {
    private Map<Class<?>, ?> capabilities;
    A() {
        capabilities.put(I1.class, new I1() {...});
        capabilities.put(I2.class, new I2() {...});
    }

    public <T> T lookup(Class<T> intf) {
        Object obj = capabilities.get(intf);
        if (obj == null) {
            throw new OperationNotSupportedException(
               "Interface not found: " + intf.getName());
        }
        return intf.cast(obj);
    }

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.