| |||||||||
Javadoc Documentation Inheritance
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:
How to inherit Javadoc documentationDocumentation 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 inheritanceFirst 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 documentationFinally, 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 situationsSince 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.
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 interfacesThe 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 inheritanceNow 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 Eclipsewhile 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. Javadoc trapsIf 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 workingReferences
SumaryJavadoc 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!CommentsIf 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 Help spread the wordShare this post on your favorite social bookmarking sites:
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... | |||||||||