Interface Syntax in Java

The code below compiles:

interface A {
    void foo();
}

interface B extends A {
    void bar();
}

class FooBar implements A, B {
    ...
}

It would be easier to say class FooBar implements B. Is there any reason besides clarity to do so? Or I might run into some problems in the future if I use the template displayed in the sample code.

+4
source share
2 answers

For the compiler, this does not matter.

For readers, readers, it can make the code more understandable in some cases. When your interface names are Animalu Mammal, declaring both is redundant because everyone knows that mammals are animals. But if the interfaces are not so closely connected, for example, DataTransferObjectand Serializable, it may be useful to specify both explicitly.

+2

, FooBar A , , , IDE .

, B extends A, FooBar , , B A.

+1

Source: https://habr.com/ru/post/1668998/


All Articles