简体   繁体   中英

Should I explicitly implement an interface when I already extend a class that implements it?

If I have an interface and an abstract class like AbstractMojo and Mojo from the Maven Plugin API, should I explicitly implement Mojo , when AbstractMojo already implements that interface?

public class MyMojo extends AbstractMojo implements Mojo {
    // like this?
    // ...
}

public class OtherMojo extends AbstractMojo {
    // or like this?
    // ...
}

Does either of these class declarations have any benefits over the other?

The question you should ask yourself is, "why would I implement something that has already been implemented in an upper hierarchical level?"

You don't need to because any abstract methods that you didn't implement in the abstract class (belonging originally in the interface), will have to implemented in your concrete class which extends the abstract class. So no point in implementing in the concrete class and the abstract class as well

A third reason to not use an extraneous implements : the reader expects all interface methods to have its first implementation here.

So a call to the super-method might be forgotten (by an inattentive developer).

It is misleading.

The generated javadoc will list all implemented interfaces.

I think you should not. You can implement AbstractMojo, but it is redundant if you have already extend class that implements it.

Everyone who uses an IDE can easily see which interfaces your class extends, even displayed in the hierarchy (eg ctrl + T, ctrl + T in Eclipse) or look in the javadoc.

Therefore, making this explicit should not be necessary and can clutter up your code if you extend many interfaces. The code is functionally identical.

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