SJB INSTITUTE OF TECHNOLOGY
Project Presentation
On
“Annotations”
By
UMESH M
Asst. Prof.
Department of Information Science and Engineering
10/22/2019 Dept of ISE, SJBIT 1
Annotations
• Built-In Java Annotations
– There are 7 built-in annotations in java. Some annotations are applied to
java code and some to other annotations.
• Built-In Java Annotations used in java code imported
from java.lang
1. @Override
2. @SuppressWarnings
3. @Deprecated
• Built-In Java Annotations used in other annotations
• 4 Annotations imported from java.lang.annotation
1. @Target
2. @Retention
3. @Inherited
4. @Documented
10/22/2019 Dept of ISE, SJBIT 2
class Animal
{
void eatSomething()
{ System.out.println("eating something"); }
}
class Dog extends Animal
{
void eatSomething( )
{ System.out.println("eating foods"); }
}
class TestAnnotation1{
public static void main(String args[ ])
{
Dog a=new Dog();
a.eatSomething();
}
}
10/22/2019 Dept of ISE, SJBIT 3
class Animal
{
void eatSomething()
{ System.out.println("eating something"); }
}
class Dog extends Animal
{
void eatsomething( ) //not overriding
{ System.out.println("eating foods"); }
}
class TestAnnotation1{
public static void main(String args[ ])
{
Dog a=new Dog();
a.eatSomething();
}
}
10/22/2019 Dept of ISE, SJBIT 4
@Override
class Animal
{
void eatSomething()
{ System.out.println("eating something"); }
}
class Dog extends Animal
{
@Override
void eatsomething( )
{ System.out.println("eating foods"); }
}
class TestAnnotation1{
public static void main(String args[ ])
{
Dog a=new Dog();
a.eatSomething();
}
}
Output:Comple Time Error
10/22/2019 Dept of ISE, SJBIT 5
@Deprecated
class A{
void m( )
{ System.out.println("hello m") ;}
@Deprecated
void n( )
{ System.out.println("hello n");}
}
class TestAnnotation3{
public static void main(String args[ ])
{
A a=new A();
a.n();
}
}
At Compile Time:
Note: Test.java uses or overrides a deprecated API.
At Runtime:
hello n
10/22/2019 Dept of ISE, SJBIT 6
@SuppressWarnings
class DeprecatedTest
{
@Deprecated
public void Display()
{
System.out.println("Deprecatedtest display()");
}
}
public class SuppressWarningTest
{
// If we comment below annotation, program generates
// warning
@SuppressWarnings("deprecation")
public static void main(String args[])
{
DeprecatedTest d1 = new DeprecatedTest();
d1.Display();
}
}
10/22/2019 Dept of ISE, SJBIT 7
10/22/2019 Dept of ISE, SJBIT 8