Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overridden methods in JavaDoc

Tags:

java

javadoc

I'm documenting a Java program that I have developed, and wanted to know if JavaDoc will be generated for an inherited method (which is overridden) if I just document the superclass?

If I have a class called Vehicle with a drive() method in, and a sub-class called Car with the drive() method overridden, will the documentation for the Vehicles drive method be included in the Cars drive method if no Javadoc is put in the Car class?

like image 808
Alex Blundell Avatar asked Apr 09 '13 14:04

Alex Blundell


People also ask

Do you write JavaDoc for overridden methods?

Yes. If you don't have javadoc comments on a subclass, javadocs will be be generated based on the superclasses javadoc. If you define javadocs in the subclass they will replace the inherited javadocs, but you can use {@inheritDoc} to include the respective superclass javadoc comments in the subclass javadocs.

What does the override method do?

Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. It allows for a specific type of polymorphism (subtyping).

Can overridden method have different return type?

The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.

How do you reference another method in JavaDoc?

Javadoc provides the @link inline tag for referencing the members in the Java classes. We can think of the @link tag as similar to the anchor tag in HTML, which is used to link one page to another via hyperlinks. Similar to the anchor tag, the path_to_member is the destination, and the label is the display text.


2 Answers

[I] wanted to know if JavaDoc will be generated for an inherited method (which is overridden) if I just document the superclass?

Yes. If you don't have javadoc comments on a subclass, javadocs will be be generated based on the superclasses javadoc.

If you define javadocs in the subclass they will replace the inherited javadocs, but you can use {@inheritDoc} to include the respective superclass javadoc comments in the subclass javadocs.

Reference:

  • https://docs.oracle.com/javase/6/docs/technotes/tools/solaris/javadoc.html#inheritingcomments
like image 194
Stephen C Avatar answered Oct 12 '22 15:10

Stephen C


If you want to use the JavaDoc of the overridden method, use {@inheritDoc}. F. e. :

/**   * {@inheritDoc}   */ @Override public double getX() { ... } 

Please notice that almost every overriden method also inherits the upper docs :) . You can read about it in the oracle docs (thanks for the hint @Steve Kuo) .

The Javadoc tool has the ability to copy or "inherit" method comments in classes and interfaces under the following two circumstances. Constructors, fields and nested classes do not inherit doc comments... (1) Automatically inherit comment to fill in missing text ... (2) Explicitly inherit comment with {@inheritDoc} tag

Use @see if you want to reference to a similar/important/... method. Example of the java.awt.Point class:

 /**  * Returns the location of this point.  * This method is included for completeness, to parallel the  * <code>getLocation</code> method of <code>Component</code>.  * @return      a copy of this point, at the same location  * @see         java.awt.Component#getLocation  * @see         java.awt.Point#setLocation(java.awt.Point)  * @see         java.awt.Point#setLocation(int, int)  * @since       1.1  */ public Point getLocation() { ... } 
like image 21
Eich Avatar answered Oct 12 '22 14:10

Eich