- 클래스 상속
package annotation.basic.inherited;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited // 클래스 상속시 자식도 애노테이션 적용
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedAnnotation {
}
package annotation.basic.inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface NoInheritedAnnotation {
}
package annotation.basic.inherited;
@InheritedAnnotation
@NoInheritedAnnotation
public class Parent {
}
package annotation.basic.inherited;
public class Child extends Parent {
}
- 인터페이스 적용
package annotation.basic.inherited;
@InheritedAnnotation
@NoInheritedAnnotation
public interface TestInterface {
}
package annotation.basic.inherited;
public class TestInterfaceImpl implements TestInterface {
}
- 실행 코드
package annotation.basic.inherited;
import java.lang.annotation.Annotation;
public class InheritedMain {
public static void main(String[] args) {
print(Parent.class);
print(Child.class);
print(TestInterface.class);
print(TestInterfaceImpl.class);
}
private static void print(Class<?> clazz) {
System.out.println("class: " + clazz);
for (Annotation annotation : clazz.getAnnotations()) {
System.out.println(" - " + annotation.annotationType().getSimpleName());
}
System.out.println();
}
}
- 실행 결과
class: class annotation.basic.inherited.Parent
- InheritedAnnotation
- NoInheritedAnnotation
class: class annotation.basic.inherited.Child
- InheritedAnnotation
class: interface annotation.basic.inherited.TestInterface
- InheritedAnnotation
- NoInheritedAnnotation
class: class annotation.basic.inherited.TestInterfaceImpl
- 클래스 상속은 @InheritedAnnotation 만 적용
- 인터페이스는 애노태이션 적용 안됨
'개발이 좋아서 > Java가 좋아서' 카테고리의 다른 글
애노테이션 - 애노테이션 검증기 (0) | 2024.12.01 |
---|---|
애노테이션 - 메타 애노테이션 (0) | 2024.12.01 |
애노테이션 - 애노테이션 정의 (0) | 2024.12.01 |
리플렉션 - 생성자 탐색과 객체 생성 (0) | 2024.12.01 |
리플렉션 - 활용 예제 (0) | 2024.12.01 |