java - Interfaces, annotations inheritance -
this question has answer here:
i've java question interfaces, annotations , 'inheritance' (let me use word if we're talking interfaces).
here's example, question:
public interface { @somekindofannotation(..) boolean modifyelement(string id) } public class b implements a{ @override public boolean modifyelement(string id){ //implementation } }
can method modifyelement(string id)
(in class b) inherit annotation @somekindofannotation
? if yes how can access annotation value?
import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy;
@retention(retentionpolicy.runtime)
public @interface somekindofannotation{
public string typeofsecurityneeded();
}
public interface {
@somekindofannotation(typeofsecurityneeded = "low") public void f3() ;
}
public class b implements {
@override public void f3() { // todo auto-generated method stub }
}
import java.lang.reflect.method;
public class testprogram {
public static void main(string[] args) { try { obj = new b(); class c = obj.getclass(); method m = c.getmethod("f3"); if(m.isannotationpresent(somekindofannotation.class)) { somekindofannotation x = m.getannotation(somekindofannotation.class); system.out.println("level of security " +x.typeofsecurityneeded() ); } else { system.out.println("no security "); } } catch (securityexception e) { e.printstacktrace(); } catch (nosuchmethodexception e) { e.printstacktrace(); } }
}
o/p -------->>>>>>>>>
no security
now add annotation in class b
public class b implements {
@override @somekindofannotation(typeofsecurityneeded = "low") public void f3() { // todo auto-generated method stub }
}
o/p ---->>>>>
level of security low
Comments
Post a Comment