Inner Classes

Q1: Which constructs an anonymous inner class instance?

A Runnable r = new Runnable() { };

B Runnable r = new Runnable(public void run() { });

C Runnable r = new Runnable { public void run(){}};

D System.out.println(new Runnable() {public void run() { }});

ANS:D - System.out.println(new Runnable() {public void run() { }});

D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable. A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation. B and C use incorrect syntax.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.