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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With