O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required