0

I searched Google but couldn't find a specific example that clears my doubt. Suppose I have a parent interface with nested interface.

E.g

public interface A {
    .. methods

    interface B {
        .. methods
    }
}

If a class implements interface A, what happens, does that class internally implements the nested interface B as well, that means am I supposed to override interface B's methods too?

Jhonny007
  • 1,698
  • 1
  • 13
  • 33
sanedroid
  • 1,036
  • 4
  • 16
  • 27
  • 1
    You can try it by declaring a method in the nested interface :) – TheLostMind Sep 23 '16 at 09:15
  • @TheLostMind - Hey thanks, the problem here is I need a quick answer, also currently I don't have resources to try out the problem in practical. If in case, I am anyway going to try using code once I reach home.. Thanks – sanedroid Sep 23 '16 at 09:30

3 Answers3

2

Since interfaces have no implementation of methods your nested interface does not need to be implemented when the outer interface gets implemented.
The inner interface is more like an interface located in a namespace of the outer interface.

To sum it up: The interfaces do not have any relation to each other you could handle them as you could two independent interfaces. The only relation is that you can only use interface B by calling A.instanceofB.method();.

Interface:

interface OuterInterface {
    String getHello();

    interface InnerInterface {
        String getWorld();
    }
}

Example:

static class OuterInterfaceImpl implements OuterInterface {
    public String getHello() { return "Hello";}
}

public static void main(String args[]) {
    new OuterInterfaceImpl().getHello(); // no problem here
}

Example 2:

static class InnterInterfaceImpl implements OuterInterface.InnerInterface {
    public String getWorld() { return "World";}
}

public static void main(String args[]) {
    new InnerInterfaceImpl().getWorld(); // no problem here
}

Example 3:

static class OuterInterfaceImpl implements OuterInterface {
    public String getHello() { return "Hello"; }

    static class InnerInterfaceImpl implements InnerInterface {
        public String getWorld() { return "World!"; }
    }
}

public static void main(String[] args) {
    OuterInterface oi = new OuterInterfaceImpl();
    OuterInterface.InnerInterface ii = new OuterInterfaceImpl.InnerInterfaceImpl();
    System.out.println(oi.getHello() + " " + ii.getWorld());
}
Jhonny007
  • 1,698
  • 1
  • 13
  • 33
0

No. The inner interface has to be implemented.

Syntax would be

Class C implements A, A.B{
// add methods here
}

If you implement only A, you are fine with only declaring A's method without B interface methods.

Rishi Goel
  • 670
  • 4
  • 10
  • I wouldn't say "The inner interface has to be implemented", but rather "If you want to use the inner interface, you need to implement it yourself." Because you can use both interfaces independently. – Jhonny007 Sep 23 '16 at 10:05
0

At basic , in an interface, anything other than a method declaration is public static. Anything which is static cannot be inherited. So, a nested interface has to be implemented separately.

Gunwant
  • 949
  • 1
  • 14
  • 29