Magazine
 
Java Developers Desk: Annotations
 

@Inherited tag by an example:

Example:

Lets first, define the annotation:

@Inherited
public @interface ParentObjectDemo {
boolean isInherited() default true;
String showSomething() default “Show
anything?”;
}

Now, annotate the class with our annotation:

@ParentObjectDemo
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

Jan  2008 | Java Jazz Up | 12
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF