Javadoc Documentation Inheritance

Author: Kasper B. Graversen, 21/07/08
Keywords: Documentation, Javadoc, Inheritance
Abstract: Documentation inheritance? It sounds like an idea from a person on drugs with a serious sleep deprivation. Never the less, it turns out in practice to be an approach to increase documentation coverage, while at same time reduce problems with documentation duplication and inconsistently updated documentation.
subscribe to my RSS feed


Bookmark and Share


Javadoc Documentation Inheritance

"Inheriting documentation! Are you on drugs?" I can hear you ask. Quite the contrary! It is a mechanism, that works very well in practice. Originally suggested in research circles in 1993 by J. Sametinger in "The role of documentation in programmer training", the idea shows to be quite old. In 2001 the university project Elastic Javadoc implemented idea along with a more flexible way of constructing Javadocs (as being dynamically depending on the needs of the programmer). What most programmers and managers alike fail to realize, is that since the Java 1.4 release in 2002, the idea has made it into main stream programming languages.

What is documentation inheritance good for? I won't blame you if your gut reaction is to think that this is an outrageous idea that "sounds nice on paper, but will never work in practice". The reality, however, is that this feature has become my preferred way of writing Javadoc! I strive to write as much documentation possible in the super classes and interfaces, and then subsequently inherit this documentation in subclasses and implementing classes.

The main benefits of this approach are the following:
  • It's easy to inherit documentation and it can be automated in most IDE's
  • It reduces documentation duplication.
  • And best of all, it keeps the documentation coverage high, making me appear to be diligent.

How to inherit Javadoc documentation

Documentation inheritance is easy. You document as usual in your super class / interface. Then, in subclasses / implementing classes, you specify to inherit the documentation either by not specifying any Javadoc, or by using the {@inheritDoc} tag. Do not get fooled by the {}, they are part of the tag name! Also notice, you can only apply the {@inheritDoc} tag to methods.

Simple documentation inheritance

First we define the documentation in an interface.
/**
 * this is some doc
 */
interface I1 {
  /**
   * this is the doc for m FROM I1
   *
   * @param a this is some parameter
   * @param b this is another parameter
   */
  void m(int a, int b);
}
Then, create a class implementing the interface and inheriting the documentation
class JustImplementing implements I1 {
  public void m(int a, int b) { ... }
}
A similar effect can be achieved using the tag explicitly.
/**
 * {@inheritDoc} doesn't have any effect here!
 */
class JustInherit implements I1 {
  /** {@inheritDoc} */
  public void m(int a, int b) { ... }
}
The tag {@inheritDoc} inserts the super documentation at the place of the tag. Hence you may extend the description of the super class in a similar fashion as you can extend functionality from a super class.
class ExtendInherited implements I1 {
  /**
   * This is the sub part I
   * <p>
   * {@inheritDoc}
   * <p>
   * And this is the sub part II
   */
  public void m(int a, int b) { ... }
}
Notice that due to a bug in Eclipse 3.3, the documentation displayed when hovering the method m will not include the super documentation! I don't know how Eclipse 3.4, IntelliJ, NetBeans or other products behave. Please send me a mail if you hold such knowledge.

Overriding parts of the documentation

Finally, it is possible to substitute part of the super documentation. Specifically, every named part of the super documentation. For example, it is possible to re-define the @param, @return and @throws documentation.
class InheritAndChangingParts implements I1 {
  /**
   * {@inheritDoc}
   *
   * @param a new documentation for the param a only
   */
  public void m(int a, int b) {...}
}

Documentation inheritance in multiple inheritance situations

Since a class may implement multiple interfaces, or implement an interface while subclassing another class, potentially multiple sets of documentation can be inherited. Surprisingly, Java does not support multiple documentation inheritance. You may only inherit one piece of documentation pr. method. The documentation is chosen based on the following lookup rules.
  • First documentation in super classes are chosen.
  • If no documentation is found, choose one from the interface. Priority is given to the interfaces in the order they are specified implemented.
Let's define another interface with a method similar to the one defined in interface I1
/**
 * this is some doc
 */
interface I2 {
  /**
   * this is the doc for another m FROM I2
   *
   * @param a parameter2
   * @param b another parameter2
   */
  void m(int a, int b);
}

Super classes are prioritized over interfaces

The following code shows that super class documentation is chosen over interface documentation
class SomeSuperClass {
  /** some super documentation */
  public void m(int a, int b) {...}
}

class MultipleDocInheritance extends SomeSuperClass implements I1 {
  /** {@inheritDoc} */
  public void m(int a, int b) { ...}
}

Interface ordering and documentation inheritance

Now lets try implementing that interface in two different ways (the ordering of the specified interfaces to implement varies).
class InheritingDocFromI1 implements I1, I2 {
  /** {@inheritDoc} */
  public void m(int a, int b) {...}
}
and
class InheritingDocFromI2 implements I2, I1 {
  /** {@inheritDoc} */
  public void m(int a, int b) {...}
}
The resulting Javadoc is for InheritingDocFromI1 the I1-documentation, whilst for InheritingDocFromI2 it is the I2-documentation that is inherited. There is another possibility for multiple documentation inheritance. Namely, when a class subclasses another class and implements an interface.

Automating inheritance in Eclipse

while the {@inheritDoc} tag strictly speaking isn't needed in order to inherit documentation, I prefer to have it explicitly stated in the code. Not only does it visually indicate that there is documentation elsewhere for the particular method, it also enable quick extension to the inherited inherited documentation as illustrated above. In the Eclipse IDE, you can automate the documentation inheritance, by telling eclipse to automatically insert the Javadoc /** {@inheritDoc} */ to all methods overriding/implementing a super class/interface. To do this select from the menu: Window:: Preferences:: Java as shown below
How to configure Eclipse to automatically specify javadoc inheritance.
How to configure Eclipse to automatically specify javadoc inheritance.

Javadoc traps

If you have trouble inheriting documentation from the Java API, remember that the API source code must be on the source path when building your Javadoc inheritDoc not working

References

Sumary

Javadoc inheritance is easy, mostly problem-free. It eliminates a lot of problems with regards to documentation duplication and inconsistent partial updates. Start inheriting your Javadoc today!



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
14/05/09 English Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
04/05/09 'Office space' extras Crazy stuff about the funny movie "Office space".
Nothing of interest? Try browsing the entire article archive...