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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

android - IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling -