简体   繁体   中英

Why implement interfaces 'A' and 'B', when 'B' extends 'A'

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  

?

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)

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.

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.

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 ).

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.

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 AB hierarchy

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM