6. Annotations in Java

Java (11 Part Series)

1 1. Introduction to Java.. Small but worthy..
2 1.1 simple hello world program
7 more parts…
3 2. Installation of Java
4 3. Exercise: Write, compile and run a Java program
5 3.2. Compile and run your Java program
6 4. Base Java language structure (Class, objects, inheritance )
7 4.2 Exception handling in Java
8 5. Java interfaces
9 6. Annotations in Java
10 7. Variables and methods
11 Important announcement

6.1. Annotations in Java

Annotations provide data about a class that is not part of the programming logic itself. They have no direct effect on the code they annotate. Other components can use this information.

Annotations can be preserved at runtime (RetentionPolicy.RUNTIME) or are only available at development time (RetentionPolicy.SOURCE).

6.2. Override methods and the @Override annotation

If a class extends another class, it inherits the methods from its superclass. If it wants to change these methods, it can override these methods, i.e., redeclare the methods. This is necessary for an abstract method unless the class itself is defined as abstract.

The @Override annotation can be added to such a method. It is used by the Java compiler to check if the annotated method really overrides a method of an interface or the extended class.

To override a method, you use the same method signature in the source code of the subclass.

To indicate to the reader of the source code and the Java compiler that you have the intention to override a method, you can use the @Override annotation.

The following code demonstrates how you can override a method from a superclass.

package com.vogella.javaintro.base;

class MyBaseClass {

public void hello() {
    System.out.println("Hello from MyBaseClass");
}

}
package com.vogella.javaintro.base;

class MyExtensionClass2 extends MyBaseClass {

@Override
public void hello() {
    System.out.println("Hello from MyExtensionClass2");
}

}
It is good practice to always use the @Override annotation. This way the Java compiler validates if you did override all methods as intended and prevents errors.

6.3. The @Deprecated annotations

The @Deprecated annotation can be used on a field, method, constructor or class and indicates that this element is outdated and should not be used anymore. Adding @Deprecated to the class does not deprecate automatically all its fields and methods.

6.4. Type annotations

Java supports that annotations can be placed on any type. The following gives several examples assuming the annotations exists.

@NonNull String name;
List names;
class UnmodifiableList implements @Readonly List {…}
email = (@Email String) input;
new @Information MyObject();
void doSomething() throws @ImportantForMe MyException { … }

Java (11 Part Series)

1 1. Introduction to Java.. Small but worthy..
2 1.1 simple hello world program
7 more parts…
3 2. Installation of Java
4 3. Exercise: Write, compile and run a Java program
5 3.2. Compile and run your Java program
6 4. Base Java language structure (Class, objects, inheritance )
7 4.2 Exception handling in Java
8 5. Java interfaces
9 6. Annotations in Java
10 7. Variables and methods
11 Important announcement

原文链接:6. Annotations in Java

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容