Introduce the concept of Anonymous Inner Classes and why they are useful.
Students should already be familiar with Abstract Base Classes and Interfaces.
Lecture: 20-40 minutes. There is currently no lab associated with this lesson. The Comparator Lab taught as part of the next lesson uses an Anonymous Inner Class.
The students will be able to use Anonymous Inner Classes instead of traditional class inheritance to create objects that are based on on-the-fly inheritance.
Prior to this lesson, students should already have been taught about Abstract Base Classes (ABCs) and Interfaces in Java.
Comparator lesson should be taught immediately after this lesson to show the utility of AICs.
Demonstrate traditional Java inheritance with this simple example:
// Java program to demonstrate Need for
// Anonymous Inner class
// Interface
interface Age {
// Defining variables and methods
int x = 21;
void getAge();
}
// Class 1
// Helper class implementing methods of Age Interface
class MyClass implements Age {
// Overriding getAge() method
@Override public void getAge()
{
// Print statement
System.out.print("Age is " + x);
}
}
Next class, we will demonstrate a practical example of using Anonymous Inner Classes with our Comparator Lab. Today, we will examine a simpler example which follows:
// Java Program to Demonstrate Anonymous inner class
// Interface
interface Age {
int x = 21;
void getAge();
}
class MyClass implements Age {
// Overriding getAge() method
@Override public void getAge()
{
// Print statement
System.out.println("Tradional Inhertiance: Your age is " + x);
}
public static void main(String [] args) {
// version 0 (standard inheritance as learned in AP CSA)
MyClass mc = new MyClass();
mc.getAge();
// version 1 (Anonymous Inner Class object)
Age a = new Age() {
@Override public void getAge() {
System.out.println("AIC Object Version: Your age is " + x);
}
};
a.getAge();
// version 2 (in-line AIC)
new Age() {
@Override public void getAge() {
System.out.println("In-line Version: Your Age is " + x);
}
}.getAge();
// version 3 (functional)
Age b = () -> System.out.println("Functional Version: Your age is" + x);
b.getAge();
}
}
After demonstrating traditional inheritance, the teacher should demonstrate the three different ways to implement AICs. They are all shown in the above example.
Declare a new object using an AIC structure (shown in the above example. Newly created object is called oj1);
Create an AIC in-line;
Show (but do not fully explain) how functional programming can be used to make an AIC. (See version 3, above)
Teacher needs to explain the following important concepts about AICs to the students:
A. Use of an Anonymous Inner Class allow us to create an object that implements (or inherits from) some interface (or ABC). It allows us to avoid having to create (and maintain) a whole new class for what is likely to be a temporary use.
B. The object created has a datatype whose class name is not defined - hence the class type is considered anonymous.
C. Anonymous Inner Classes can be implemented not only on an Interface but also on an Abstract Base Class.
D. methods of an anonymous class have direct access to all the members of the enclosing classes, including private members.