Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments on Overridden method in Java

Should we comment the overridden method or not? If yes, then whether the comment will be a Java Doc or simple comment?

like image 589
Mudassir Avatar asked Nov 30 '10 05:11

Mudassir


People also ask

Do overridden methods need javadocs?

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

What is an overridden method in Java?

In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

How do you tell if a method is overridden?

getMethod("myMethod"). getDeclaringClass(); If the class that's returned is your own, then it's not overridden; if it's something else, that subclass has overridden it.

Can you call an overridden method?

Invoking overridden method from sub-class : We can call parent class method in overriding method using super keyword. Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name(Constructor name must always be same as Class name).


1 Answers

@SimonC's answer explains how the javadoc utility generates "inherited" documentation for overridden methods.

You can also put explicit javadocs in an override method and they will take precedence over the inherited javadocs. Furthermore, if you put the {@inheritDoc} tag in the override method's explicit javadocs, the inherited comments will be included at that point.

To answer this:

Should we comment the overridden method or not? If yes, then whether the comment will be a Java Doc or simple comment?

In my opinion, if the override method refines the documented semantics (contract) of the overridden method (or ... heaven forbid ... breaks the contract), then this deserves to be documented in the override method's javadocs. However, if the differences are merely "implementation details", then simple comments (or no comments) are more appropriate.

(However, the practice of including a "non-javadoc" comment that refers the reader back to the overridden method's javadoc is, IMO, a waste of screen real-estate ... when I am reading the source code.)

like image 93
Stephen C Avatar answered Sep 21 '22 04:09

Stephen C