Inheriting Javadoc Method Comments
Learn how you can set up your Javadoc method comments in order to automatically — partially or fully — pass on their documentation.
Join the DZone community and get the full member experience.
Join For Free
although the
jdk tools and utilities
pages for the
javadoc tool
describe the rules of javadoc method comment reuse by implementing and inheriting methods, it is easy to unnecessarily explicitly describe comment inheritance with
{@inheritdoc}
when it's not really needed because the same comments would be implicitly inherited. the
java 8 javadoc tool page
describes the rules of inherited method javadoc comments under the section "
method common inheritance
" and the
java 7 javadoc tool page
similarly describes these rules under the section "
automatic copying of method comments
." this post uses simple code examples to illustrate some of the key rules of javadoc method comment inheritance.
the following interfaces and classes are contrived examples that will be used in this post to illustrate inheritance of javadoc comments on methods. some inherited/implementing methods include their own javadoc comments that override parent's/interface's methods comments fully or partially and other simply reuse the parent's/interface's methods' documentation.
herbivorous interface
package dustin.examples.inheritance;
/**
* marks animals that eat plants.
*/
public interface herbivorous
{
/**
* eat the provided plant.
*
* @param planttobeeaten plant that will be eaten.
*/
void eat(plant planttobeeaten);
}
carnivorous interface
package dustin.examples.inheritance;
/**
* marks an animal that eats other animals.
*/
public interface carnivorous
{
/**
* eat the provided animal.
*
* @param animalbeingeaten animal that will be eaten.
*/
void eat(animal animalbeingeaten);
}
omnivorous interface
package dustin.examples.inheritance;
/**
* eats plants and animals.
*/
public interface omnivorous extends carnivorous, herbivorous
{
}
viviparous interface
package dustin.examples.inheritance;
/**
* mammals that give birth to young that develop within
* the mother's body.
*/
public interface viviparous
{
/**
* give birth to indicated number of offspring.
*
* @param numberofoffspring number of offspring being born.
*/
void givebirth(int numberofoffspring);
}
animal class
package dustin.examples.inheritance;
/**
* animal.
*/
public abstract class animal
{
/**
* breathe.
*/
public void breathe()
{
}
/**
* communicate verbally.
*/
public abstract void verballycommunicate();
}
mammal class
package dustin.examples.inheritance;
/**
* mammal.
*/
public abstract class mammal extends animal
{
}
mammalwithhair class
package dustin.examples.inheritance;
import java.awt.*;
/**
* mammal with hair (most mammals other than dolphins and whales).
*/
public abstract class mammalwithhair extends mammal
{
/** provide mammal's hair color. */
public abstract color gethaircolor();
}
dog class
package dustin.examples.inheritance;
import java.awt.color;
import static java.lang.system.out;
/**
* canine and man's best friend.
*/
public class dog extends mammalwithhair implements omnivorous, viviparous
{
private final color haircolor = null;
/**
* {@inheritdoc}
* @param otheranimal tasty treat.
*/
@override
public void eat(final animal otheranimal)
{
}
/**
* {@inheritdoc}
* @param planttobeeaten plant that this dog will eat.
*/
@override
public void eat(final plant planttobeeaten)
{
}
/**
* {@inheritdoc}
* bark.
*/
public void verballycommunicate()
{
out.println("woof!");
}
/**
* {@inheritdoc}
* @param numberpuppies number of puppies being born.
*/
@override
public void givebirth(final int numberpuppies)
{
}
/**
* provide the color of the dog's hair.
*
* @return color of the dog's fur.
*/
@override
public color gethaircolor()
{
return haircolor;
}
}
cat class
package dustin.examples.inheritance;
import java.awt.color;
import static java.lang.system.out;
/**
* feline.
*/
public class cat extends mammalwithhair implements carnivorous, viviparous
{
private final color haircolor = null;
/**
* {@inheritdoc}
*/
@override
public void eat(final animal otheranimal)
{
}
@override
public void verballycommunicate()
{
out.println("meow");
}
@override
public void givebirth(int numberkittens)
{
}
@override
public color gethaircolor()
{
return haircolor;
}
}
horse class
package dustin.examples.inheritance;
import java.awt.color;
import static java.lang.system.out;
/**
* equine.
*/
public class horse extends mammalwithhair implements herbivorous, viviparous
{
private final color haircolor = null;
/**
* @param plant plant to be eaten by this horse.
*/
@override
public void eat(final plant plant)
{
}
/**
*
*/
@override
public void verballycommunicate()
{
out.println("neigh");
}
/**
* @param numbercolts number of colts to be born to horse.
*/
@override
public void givebirth(int numbercolts)
{
}
@override
public color gethaircolor()
{
return haircolor;
}
}
the next screen snapshot shows the contents of the package that includes the interfaces and classes whose code listings are shown above (not all the classes and interfaces in the package had their code listings shown).
the three classes of most interest here from methods' javadoc perspective are the classes
dog
,
cat
, and
horse
because they implement several interfaces and extend
mammalwithhair
, which extends
mammal
, which extends
animal
.
the next screen snapshot is of the javadoc for the
animal
class rendered in a web browser.
the
animal
class doesn't inherit any methods from a superclass and doesn't implement any methods from an interface and is not very interesting for this blog post's topic. however, other classes shown here extend this class and so it is interesting to see how its method comments affect the inheriting classes' methods' descriptions.
the next two screen snapshots are of the javadoc for the
mammal
and
mammalwithhair
classes as rendered in a web browser. there are no javadoc comments on any significance on
mammal
, but there is one method comment for a new method introduced by
mammalwithhair
.
the next three screenshots are of subsets of javadoc documentation in a web browser for the interfaces
herbivorous
,
carnivorous
, and
omnivorous
. these interfaces provide documentation for methods that will be inherited by classes that implement these methods.
with the generated javadoc methods documentation for the parent classes and the interfaces shown, it's now time to look at the generated documentation for the methods of the classes extending those classes and implementing those interfaces.
the methods in the
dog
class shown earlier generally used
{@inheritdoc}
in conjunction with additional text. the results of inheriting method javadoc comments from extended classes and implemented interfaces combined with additional test provided in
dog
's comments are shown in the next screen snapshots.
the last set of screen snapshots demonstrates that the
dog
class's documentation mixes the documentation of its "parents" with its own specific documentation. this is not surprising. the
dog
class's methods generally explicitly inherited javadoc documentation from the parents (base classes and interfaces), but the
cat
class mostly has no javadoc comments on its methods, except for the
eat
method, which simply uses
{@inheritdoc}
. the generated web browser output from this class is shown in the next screen snapshots.
the methods in
cat
that had no javadoc comments applied show up in the generated web browser documentation with documentation inherited from their base classes or interfaces and the documentation on these methods includes the phrase "description copied from class:" or "description copied from interface:" as appropriate. the one
cat
method that does explicitly include the documentation tag
{@inheritdoc}
does copy the parent's method's documentation, but does not include the "description copied from" message.
the
horse
class's methods are generally not documented at all and so their generated documentation includes the message "description copied from...". the
eat()
and
givebirth()
methods of the
horse
class override the
@param
portion and so the parameter documentation for these two methods in the generated web browser documentation (shown in the next set of screen snapshots) is specific to
horse
.
from the above code listings and screen snapshots of generated documentation from that code, some observations can be made regarding the inheritance of methods' javadoc comments by extending and implementing classes. these observations are also described in the javadoc tool documentation :
-
javadoc comments are inherited from parent class's methods and from implemented interface methods either implicitly when no text is specified (no javadoc at all or empty javadoc
/** */
).-
javadoc documentation
: "the
javadoc
command allows method comment inheritance in classes and interfaces to fill in missing text or to explicitly inherit method comments."
-
javadoc documentation
: "the
-
use
{@inheritdoc}
explicitly states that comments should be inherited.-
javadoc documentation
: "insert the
{@inheritdoc}
inline tag in a method main description or@return
,@param
, or@throws
tag comment. the corresponding inherited main description or tag comment is copied into that spot."
-
javadoc documentation
: "insert the
-
implicit and explicit inheritance of method documentation can be achieved in combination by using
{@inheritdoc}
tags in different locations within the method comment.
given the above observations and given the advertised " method comments algorithm ", a good rule of thumb for writing javadoc from the perspective of the html generated from the javadoc is to define general comments at as high a level as possible and allow automatic inheritance of the extended classes's and implemented interfaces' methods' javadoc documentation to take place, adding or overriding only portions of a method's javadoc text that are necessary to clarify or enhance the description for a lower-level method. this is better than copying and pasting the same comment on all methods in an inheritance or implementation hierarchy and needing to then keep them all updated together.
this post has focused on the web browser presentation of generated javadoc methods' documentation. fortunately, the most commonly used java ides ( netbeans [ ctrl+hover ], intellij idea [ ctrl+q / settings ], eclipse [ f2 / hover / javadoc view ], and jdeveloper [ ctrl-d ]) support presentation of javadoc that generally follows the same rules of method documentation inheritance. this means that java developers can often write less documentation and almost entirely avoid repeated documentation in inheritance and implementation hierarchies.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments