Just as one does not copy and paste all of the methods from the superclass when writing code, so to should one not copy and paste all of the documentation when documenting overridden methods.
From javadoc at docs.oracle.com:
{@inheritDoc}
Inherits (copies) documentation from the "nearest" inheritable class or implementable interface into the current doc comment at this tag's location. This allows you to write more general comments higher up the inheritance tree, and to write around the copied text.
This tag is valid only in these places in a doc comment:
- In the main description block of a method. In this case, the main description is copied from a class or interface up the hierarchy.
- In the text arguments of the
@return
, @param
and @throws
tags of a method. In this case, the tag text is copied from the corresponding tag up the hierarchy.
See Automatic Copying of Method Comments for a more precise description of how comments are found in the inheritance hierarchy. Note that if this tag is missing, the comment is or is not automatically inherited according to rules described in that section.
And thus, you inherit the documentation from the superclass. If the superclass documentation updates, yours does too.
Just as with inheritance of code, you can extend the comments.
Lets see what things look like in an IDE and code...
public interface IFace {
/**
* This is a method.
*/
public void method1();
/**
* This is also a method
*/
public void method2();
}
This is just an interface with some javadocs and two methods.
Lets have a class implement this...
public class CClass implements IFace {
/**
* {@inheritDoc}
* This just returns.
*/
public void method1() {
}
public void method2() {
}
}
And then the javadocs themselves:
public void method1()
This is a method. This just returns.
Specified by:
method1 in interface IFace
img
public void method2()
Description copied from interface: IFace This is also a method
Specified by:
method2 in interface IFace
img
The first thing to see is that with method1
, the javadoc is included and extended with the {@inheritdoc}
. However, no javadoc will also inherit the javadoc from wherever.
If you are not changing the javadoc, don't specify it. Let the IDE do its thing. Let the automatic javadoc generation do its thing too. Copying and pasting documentation can be just as bad as copying and pasting code as it violates DRY and may lead to the documentation from the superclass or interface getting out of sync with the documentation of the method.
On the other hand, if you are going to write something more, inherit the documentation and write it.