What will be the output of the following code?

package question;

import java.lang.reflect.Method;


class Test {

    public void methodPublic() {
        System.out.println("Test.methodPublic");
    }

    protected void methodProtected() {
        System.out.println("Test.methodProtected");
    }

    private void methodPrivate() {
        System.out.println("Test.methodPrivate");
    }
}

public class PrivateTest {

    public static void main(String[] args) throws Exception {
        Test test = new Test();
        callMethods(test, "methodPublic");
        callMethods(test, "methodProtected");
        callMethods(test, "methodPrivate");
    }


    static void callMethods(Object a, String methodName) throws Exception {
        Method m = a.getClass().getDeclaredMethod(methodName);
        m.setAccessible(true);
        m.invoke(a);
    }

}

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes