CSC302 2006S, Class 20: Anonymous Inner Classes Admin: * Class Friday: Bjarne Stroustrup on C++ * After the movie, submit three most important things you learned * No class Monday. * Questions on the homework? Overview: * Anonymous values. * Encapsulation and inner classes. * Anonymous inner classes. * Applications. /Notes on the Homework/ * When computing the meaning of a statement, * You get: Statement, "Remainder of Program", Environment, Input * You must generate: Output (for your statement and the remainder of the programm) * Typical activities: * Update input (e.g., remove the first element) * Update the environment (e.g., map an identifier to a value) * Call the continuation * Extend the output from the continuation * When computing the meaning of an expression * You get: Statement, environment * You must generate: A number * When computing the meaning of a list of statements * You get: List of statements, "Remainder of program", Environment, Input * You must generate: Output (for the list of statements and the remainder of the program) * Recursion is permitted! /Anonymous Values/ * Question: Why high-level languages? * Easier to read * Faster * Relieve programmer of unnecessary burdens * Rewriting similar code * Implementing previously implemented stuff * Support abstraction * One technique to abstract (and ease burdens): Don't require names for things that don't require names * E.g., Compute x = (a/b + b )/2; * We don't have to name the "hidden" temporaries * In functional programming, we have anonymous functions * Anonymous functions are functions without names * Examples ... * That equation I wrote above * (map! (lambda (x) (+ 5 x)) grades) vs. (let ((add5 (lambda (x) (+ 5 x)))) (map! add5 grades)) * Also (map! (left-section + 5) grades) * Natural implication: Object-oriented languages should have anonymous objects and anonymous classes * Anonymous objects foo(new Whatever(...)); str1.concat(str2).concat(str3); * Anonymous classes * Classes when you only want to create one object (or a few) and don't need names /Encapsulation and Inner Classes/ * Encapsulation * Putting things within something else * E.g., methods, fields within a class * Containment * Protecting and hiding data * Inner classes: * Classes accessible to only one class * Reveal some of enclosing to inner class in a controlled way /Anonymous Inner Classes/ new SuperClass() { overriddenmehotds() ... } * Inner classes: Can reference some fields and methods of enclosing cass * Anonymous: No constructor /Your Applications/ * Callbacks * Pass something (kinda like a method) to another object that it can call to affect parts of your class * Example "When someone clicks on this button, highlight this other button" * Event handling * Coroutining * Similar ideas * Things you wouldn't normally make as objects * Something with lots of unique parts /My Applications/ * The places in which you want to do functional programming sort(students, new Comparator() { public int compare(Student a, Student b) { return a.lastName.compareTo(b.lastName); } });