Multiple inheritance on Java interfaces

Jacob Wallace picture Jacob Wallace · Nov 15, 2012 · Viewed 39k times · Source

I thought multiple inheritance was always illegal in Java, but this code compiles:

public interface A {
  void a();
}

public interface B {
  void b();
}

public interface AB extends A, B {
}

Would having an empty interface such as AB be considered a bad practice? Is there a way to achieve something similar while avoiding the empty interface (using generics or otherwise)?

Note: I'm not asking how to simulate multiple inheritance via interfaces. I realize I could do the following:

public class AbImpl implements A, B {
  public void a() {}
  public void b() {}
}

For various reasons I need an interface that has both methods.

Answer

Brian Agnew picture Brian Agnew · Nov 15, 2012

Multiple inheritance of implementations is not allowed. Components can inherit multiple interfaces, though.

Inheriting multiple interfaces isn't problematic, since you're simply defining new method signatures to be implemented. It's the inheritance of multiple copies of functionality that is traditionally viewed as causing problems, or at the very least, confusion (e.g., the diamond of death).