4

Suppose I have the following code...

interface A{
  void a();
}

interface B extends A{
  void b();
}

class ImplementOne implements B{ 
  public void a(){};
  public void b(){};
}     

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

Regardless of whether class ImplementTwo implements both B and A, or just B, it would still need to implement method a() in interface A, since interface B extends interface A. Is there any reason one would explicitly do

...implements B, A

instead of just

...implements B  

?

M A
  • 71,713
  • 13
  • 134
  • 174
Skogen
  • 721
  • 1
  • 11
  • 30
  • 5
    No particular reason beyond code style. – Matt Ball Sep 09 '14 at 13:24
  • Look at existing awnsers: http://stackoverflow.com/questions/10839131/implement-vs-extends-when-to-use-whats-the-difference – PieterSchool Sep 09 '14 at 13:24
  • @PieterSchool that question doesn't answer this question, OP is not asking about the difference of extension vs. implementation, but about the stylistic difference of specifying multiple interfaces which extend each other. – WillBD Sep 09 '14 at 13:30
  • Related: [why-does-arraylist-have-implements-list](http://stackoverflow.com/questions/4387419/why-does-arraylist-have-implements-list) – Pshemo Sep 09 '14 at 13:39
  • Does reimplementing an interface change which default implementation will be used when multiple defaults are available? – Jeffrey Bosboom Sep 09 '14 at 13:47

7 Answers7

5

There is no difference between the two approaches in terms of behavior. In terms of bytecode information, there is a small difference when it comes to information about implemented interfaces. For example:

Class<?>[] interfaces = ImplementTwo.class.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
    System.out.println(interfaces[i]);
}

would return two class instances when implements B, A is used, whereas when using implements B it would return one instance.

Still, the following returns true using both approaches:

A.class.isAssignableFrom(ImplementTwo.class)
M A
  • 71,713
  • 13
  • 134
  • 174
2

IMO the only reason you would want to specify it explicitly like that is if you were attempting to make the code more easily readable by others who needed to interact with it. That even being said, really, a two-step indirection like this is not so abstract that it's difficult to follow, so I don't really think this would ever have a need to happen.

WillBD
  • 1,919
  • 1
  • 18
  • 26
  • +1 A common reason this happens in the JDK is that it was added at one point and they are very reluctant to change something like this in case it break someone's code. i.e. with so much code out there, it is highly likely some one has written code which depends on this e.g. via reflection. – Peter Lawrey Sep 09 '14 at 14:04
  • I bet that in the past, interface B did not extend A, then later some realised that B should extend A. They then fixed 1 or 2 compiler errors for those implementations missing the 'a' method, while not bothering to modify the possibly numerous classes that already declared "implements A, B". – DJDaveMark Aug 24 '17 at 14:17
2

The most famous example is of the use of the interface Serializable.

This is often repeated for the purpose of: Should the super interface suddenly gets detached from Serializable interface, it's sub-interface will still remain Serializable since it's already defined as Serializable.

This often occurs doing code refactoring. Other than that, there is no difference.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
1

Both variants are exactly the same semantically. You might prefer one over the other for stylistic reasons (for example, to make it immediately clear to the reader of the class that it implements both A and B).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

There is no difference in how the code will behave (or compile).

Some people prefer explicitly listing all implemented interfaces even if they are enforced by another interface implemented. It's purely a matter of personal/code style preferences.

Patrick Huy
  • 975
  • 1
  • 8
  • 19
0

Both approaches are equal. You might choose implements A, B instead of implements B to specify whole list of types for object ithout knowledge about A-B hierarchy

mishadoff
  • 10,719
  • 2
  • 33
  • 55
0

In this case it doesn't make difference. But technically you could have two interfaces which are not related one to other with same method declaration. And this implementation would implement method for both interfaces.

Segg3r
  • 466
  • 3
  • 6