7. Nested Classes and Interfaces

7.1
 // Filename: Exercise3.java interface Function { public int evaluate(int arg); } class Half implements Function { public int evaluate(int arg) { return arg/2; } } class Print implements Function { public int evaluate(int arg) { System.out.println(arg); return arg; } } public class Exercise3 { /* Inner class that applies the function, prints the value, and returns the result. */ static class PrintFunc extends Print { PrintFunc(Function f) { func = f; } Function func; public int evaluate(int arg) { return super.evaluate(func.evaluate(arg)); } } // Inner class that just returns the argument unchanged. /* Use this when you want a PrintFunc object to print the argument as-is. */ static class NoOpFunc implements ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.