Professional Documents
Culture Documents
Sathish
-1-
Naresh i Technologies
Sathish
-2-
Naresh i Technologies
Sathish
@Override
public void doTheThing() {
System.out.println("Do it differently");
}
}
In case the method doTheThing() in MySuperClass changes signature so
that
the same method in the supclass no longer overrides it, the compiler will
generate an error.
so A method annotated with @Override must override a method from
super class. other wise we will get Compiletime Error
SourceCode:@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override
{
}
@Deprecated:java provides a mechanism to express deprecation . As new classes
invented its API changes naturally, method names are renamed for
consistenecy ,new and better methods are added may change. some
changes introduce problems ,
we need to keep the old API avilable until developers habituated
to new one.But we don't want to continue them in the future
The Word Deprecated generates a warring when we use deprecated
elements.
@Documented
@Retention(RetionPolicy:RUNTIME)
public @interface Deprecated{
}
@SuppressWarinings:SuppressWarinings annotation indicates to the compilerto suppress(or)
remove the warining messages.
Meta Annotation:1) @Inherited:In general in java app annotations are not inherited from super class to
sub-class by default. if we want make any annotation to inherit from
super to sub class we have to @Inherited Annotation.
Ex:@Inherited
@interface Table{
}
@Table
-3-
Naresh i Technologies
Sathish
class Emp{
}
class Manager extends Emp
{
}
2) Documented Annotation:In java app by default all annotations are not documented when we
prepare documentation for our java app.
if we want to make any annotation to incldue in the documentation we
have to use @Documented Annotation.
3) @Target
The mainpurpose of this annotation is to define the target elements for
the annotations at the time of declrations.
This annotation is multivalue annotation,it will take the following
constants from element type as parameter.
TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE,PACKA
GE. ALL
Ex:@Target(ElementType.METHOD,ElementType.TYPE,ElementType.FIELD)
4) Retention-Annotation:This annotation can be used to define a particular scope to some other
Annotation,here the scope of annotation may be source file(or) upto .class
file
(or) upto run time.
Ex:@Retention(RetentionPolicy.RUNTIME)
Custom Annotations in Java
Creating custom annotation is similar to writing an interface, except that
it interface keyword is prefixed with @ symbol.
Example ://course.java
import java.lang.annotation.*;
@Inherited
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Course{
-4-
Naresh i Technologies
Sathish
-5-