View RSS Feed

My Java Tips

Setting up inheritance

Rate this Entry
by , 10-31-2011 at 05:19 PM (459 Views)
Inherited is a meta-annotation that is complicated as compared to other annotations. It is least-often used.


Let me present a scenarios for better understanding: Suppose that you use your custom annotation called InProgress to mark a class as being in progress.
If the Documented meta-annotation is applied correctly, then this will show up in the Javadoc. All is well till here.

Suppose you write a new class and extend the in-progress class. Now remember one this: the super class is in progress. What about the subclass? For subclass, there will be no indication even in its documentation, that it is incomplete. One would expect to see the InProgress annotation carried through to subclasses. But its not done here.


I will now present an example showing how can we use the Inherited meta-annotation to specify the behavior we want.



Java Code:
package com.domain.tiger.a;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// Using the Inherited meta-annotation
/**
 * Marker annotation to indicate that a method or class
 *   is still in progress.
 */
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InProgress { }
We addition of @Inherited, and this will show up the InProgress annotation on subclasses of annotated classes.

Submit "Setting up inheritance" to Facebook Submit "Setting up inheritance" to Digg Submit "Setting up inheritance" to del.icio.us Submit "Setting up inheritance" to StumbleUpon Submit "Setting up inheritance" to Google

Tags: None Add / Edit Tags
Categories
Annotations

Comments