How to retain annotation of default method of interface after implementing and overriding said method

When I am overriding a default method of an interface which is annotated, the annotation is removed from the child method. I want to retain the annotation in the child even when the method is overridden. What is the reason for it getting removed?

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation {     public String config(); } 


public interface MyInterface {          @MyAnnotation(config="myConfig")     public default String getMyVal() {         return null;     }          @MyAnnotation(config="myConfig")     public default String getMyVal2() {         return null;     } }

public class MyModel implements MyInterface {     @Override     public String getMyVal() {         return "MyVal";     } }

public class MainRunner {     public static void main(String[] args) {                 method ->> System.out.println(method + " --- " + ann.annotationType()))                 );     } } 


public native int java.lang.Object.hashCode() — interface jdk.internal.HotSpotIntrinsicCandidate public final native java.lang.Class java.lang.Object.getClass() — interface jdk.internal.HotSpotIntrinsicCandidate public final native void java.lang.Object.notify() — interface jdk.internal.HotSpotIntrinsicCandidate public final native void java.lang.Object.notifyAll() — interface jdk.internal.HotSpotIntrinsicCandidate public default java.lang.String com.javaconcepts.annotations.MyInterface.getMyVal2() — interface com.javaconcepts.annotations.MyAnnotation

Appreciate the help. Thanks.

PS: I tried searching but having overridden and annotations in the search gave results for overridden annotation.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.