File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Dout on @inherited Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dout on @inherited" Watch "Dout on @inherited" New topic

Dout on @inherited

Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

I have been doing some reading about annotations for various web sites & have a fairly good understanding about them now. However this text about the @inherited annotation left be a little puzzled

nherited Annotation: This annotation is little bit complex. It inherits the annotated class automatically. If you specify @Inherited tag before defining a class then apply the annotation at your class and finally extend the class then the child class inherits the properties of the parent class automatically. Lets demonstrate the benefits of using the @Inherited tag by an example:


Lets first, define the annotation:

public @interface ParentObjectDemo {
boolean isInherited() default true;
String showSomething() default "Show anything?";

Now, annotate the class with our annotation:

public Class ChildObjectDemo {

The above example shows that you do not need to define the interface methods inside the implemented class. The @Inherited tag automatically inherits the methods for you. Suppose you define the implementing class in the old-fashioned-java-style then let us see the effect of doing this:

public class ChildObjectDemo implements ParentObjectDemo {
public boolean isInherited() {
return false;
public String showSomething() {
return "";
public boolean equals(Object obj) {
return false;
public int hashCode() {
return 0;
public String toString() {
return "";
public Class annotationType() {
return null;

Have you seen the difference? You have to implement all the methods of the parent interface. You will have to implement the equals(), toString(), and the hashCode() methods of the Object class and also the annotation type method of the java.lang.annotation.Annotation class. You will also have to include all these methods in your class regardless of whether you are implementing all these methods or not.

Can anyone please correct and/or explain in a simpler manner what the author of this text is trying to explain.

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

This is not related to SCBCD. Moving to Java Intermediate.

[My Blog]
All roads lead to JavaRanch
I agree. Here's the link:
subject: Dout on @inherited
It's not a secret anymore!