java 8 - how to call default method in interface use reflection -


public interface testserviceiface {     default string test(string str, int flag) {         return str;     } } 

interface this,if implements interface, , have instance ,how can call default method? if use reflection, how do? , have interface,no impl class , no impl instance.how call default method?

you can access interface default methods reflection below:

class<testserviceiface> type = testserviceiface.class;  method defaultmethod = type.getmethod("test", string.class, int.class);  string result = (string) defaultmethod.invoke(instance, "foo", 0); 

however, if subclass override default method, overrided method called, means interface default method supports polymorphism.


Comments

Popular posts from this blog

python - Best design pattern for collection of objects -

go - serving up pdfs using golang -

python - django admin: changing the way a field (w/ relationship to another model) is submitted on a form so that it can be submitted multiple times -