oop

17. Anonymous Inner Classes: On-the-Fly

Analyze the code, write your predicted output, and then reveal the solution.

Scenario

Sometimes you need a class just once. Instead of creating a new .java file, you can extend a class or implement an interface "on-the-fly".

Code Block
abstract class Button { abstract void onClick(); } public class Main { public static void main(String[] args) { // Creating an anonymous subclass of Button Button loginBtn = new Button() { @Override void onClick() { System.out.println("User Logged In!"); } }; loginBtn.onClick(); System.out.println("Class Name: " + loginBtn.getClass().getName()); } }
Take Your Shot
Predicted Output

Type exactly what you think will be printed in the console.

17. Anonymous Inner Classes: On-the-Fly | DevExCode