> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Inherited Annotation

The `@Inherited` meta-annotation in Java specifies that an annotation applied to a class is automatically inherited by its subclasses. By default, annotations in Java are not inherited. Applying `@Inherited` to a custom annotation definition alters this behavior, instructing the JVM's reflection mechanism to traverse up the class hierarchy to locate the annotation if it is not explicitly present on the queried subclass.

## Syntax and Declaration

To create an inherited annotation, apply `java.lang.annotation.Inherited` to the annotation declaration. Because inheritance resolution typically occurs dynamically, it is almost always paired with `@Retention(RetentionPolicy.RUNTIME)`.

```java theme={"dark"}
import java.lang.annotation.*;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface InheritedAnnotation {
    String value() default "default";
}
```

## Technical Mechanics and Limitations

The behavior of `@Inherited` is strictly governed by the Java Language Specification and operates under specific constraints:

1. **Class-Level Restriction:** `@Inherited` only affects annotations applied to class declarations (`ElementType.TYPE`). It has absolutely no effect if the annotation is applied to methods, fields, constructors, or local variables. Subclass methods do not inherit annotations from overridden superclass methods, regardless of the `@Inherited` meta-annotation.
2. **Class Extension vs. Interface Implementation:** The inheritance mechanism strictly follows class extension (`extends`). It does **not** apply to interface implementation (`implements`).
   * If an interface is annotated with an `@Inherited` annotation, classes that implement that interface will *not* inherit the annotation.
   * If a sub-interface extends a super-interface annotated with an `@Inherited` annotation, the sub-interface will *not* inherit the annotation.
3. **Reflection Resolution:** The inheritance is resolved at runtime via the `java.lang.Class` reflection API. When `Class.getAnnotation(Class<A> annotationClass)` is invoked:
   * The JVM first checks if the annotation is directly present on the target class.
   * If absent, and the annotation type is meta-annotated with `@Inherited`, the JVM queries the superclass.
   * This recursive traversal continues up the inheritance tree until the annotation is found or `java.lang.Object` is reached.

## Behavior Visualization

The following code demonstrates the strict class-hierarchy resolution of `@Inherited`:

```java theme={"dark"}
// 1. Superclass application
@InheritedAnnotation("SuperClassData")
public class SuperClass { }

// SubClass inherits the annotation from SuperClass
public class SubClass extends SuperClass { }

// 2. Interface application
@InheritedAnnotation("InterfaceData")
public interface BaseInterface { }

// ImplementingClass DOES NOT inherit the annotation
public class ImplementingClass implements BaseInterface { }
```

If you query these classes using the Reflection API, the results align with the inheritance rules:

```java theme={"dark"}
// Returns true
boolean hasSubClassAnnotation = SubClass.class.isAnnotationPresent(InheritedAnnotation.class);

// Returns "SuperClassData"
InheritedAnnotation subClassAnn = SubClass.class.getAnnotation(InheritedAnnotation.class);

// Returns false (Interfaces do not pass down inherited annotations)
boolean hasImplAnnotation = ImplementingClass.class.isAnnotationPresent(InheritedAnnotation.class);
```

## Annotation Overriding

If a subclass explicitly declares the same annotation that it would otherwise inherit, the subclass's explicit annotation shadows (overrides) the superclass's annotation. The reflection API will return the annotation directly present on the subclass, halting the upward traversal of the class hierarchy.

```java theme={"dark"}
@InheritedAnnotation("Parent")
public class ParentClass { }

@InheritedAnnotation("Child")
public class ChildClass extends ParentClass { }

// Reflection on ChildClass.class.getAnnotation(InheritedAnnotation.class) 
// will yield an annotation with the value "Child".
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
