• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regarding Inherited Annotation

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using following code to use Inherited Annotaion:


------TestAnnotation.java----------------------------------------------
import java.lang.annotation.Inherited;

@Inherited
public @interface TestAnnotation {
boolean isInherited() default true;
String doSomething() default "Do what?";
}

---------------------------------------------------------------------------
---------AnnotationChild.java------------------------------------------

@TestAnnotation
public class AnnotationChild {

public static void main(String [] args){

AnnotationChild ta = new AnnotationChild();
System.out.println("hello"+ta.doSomething());
}
}
--------------------------------------------------------------------------------
now i am getting compile time error.
The method doSomething() is undefined for the type AnnotationChild

i am not able to use the interface properties.
please guide where i m wrong.

Thanks
Ritesh
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations are not on the SCJP exam, so your post does not belong in the SCJP forum. I will move it to a more appropriate forum for you.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotation methods are not inherited this way. The annotation itself is inherited, and you can retrieve it using reflection. For example:
However, the values are the same for all instances of the class; you can't have one instance return false for isInherited() and another true.

This example requires regular interfaces:
Or, if you're lazy, use an abstract class in between:

I think you should read http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html, it may help you understand annotations better.
 
Ritesh Raman
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Can you please explain more below line (The annotation itself is inherited).how can Annotation methods are inherited ?
How can we use '@inherited'.if possible please explain with example:

!--Annotation methods are not inherited this way. [b]The annotation itself is inherited, and you can retrieve it using reflection. For example:

i have used the below line in our code , it give NullPointerException at the line no. 3. Pease explain:

1.Class<?> cls = ta.getClass();
2.TestAnnotation anno = cls.getAnnotation(TestAnnotation.class);
3.System.out.println(anno.doSomething()); --![/b]

Thanks
Ritesh
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, right. The retention policy defaults to RetentionPolicy.CLASS. You'll need to change your annotation a bit:
 
That new kid is a freak. Show him this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic