Should we comment the overridden method or not? If yes, then whether the comment will be a Java Doc or simple comment?
Yes. If you don't have javadoc comments on a subclass, javadocs will be be generated based on the superclasses javadoc.
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.
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.
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).
@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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With