Kotlinlearncs.online LogoJava

Anonymous Classes

interface Adder {
int addTo(int value);
}
Adder addOne = new Adder() {
@Override
public int addTo(int value) {
return value + 1;
}
};
Adder addEight = new Adder() {
@Override
public int addTo(int value) {
return value + 8;
}
};
System.out.println(addOne.addTo(1));
System.out.println(addEight.addTo(1));
System.out.println(addOne.addTo(8));

This lesson ventures into interesting uncharted territory. Until now, our classes have needed to have names. But, Java doesn’t actually require this! Let’s explore anonymous classes and their uses…

Please note that the next two lessons are on fairly advanced topics. You will see and need to understand code that uses these ideas, but testing on them will be limited.

Warm Up Debugging Challenge
Warm Up Debugging Challenge

But… hold on! Let’s warm up with another debugging challenge!

Create and complete the implementation of the Circle class. Your class should be public, not final and not abstract, inherit from the Shape class, and provide the following methods:

  1. Constructor that takes a double parameter. Creates a new Circle with the passed radius. You can assume that the passed radius is greater than zero. You should call the Shape constructor and pass it the String "circle" to identify the type of this shape.
  2. Public instance method area that takes no arguments and returns a double. Return the area of this shape: Math.PI * radius * radius.
  3. Override public boolean equals(Object other). Return true if other is a Circle with the same radius, and false otherwise. Note that other may be null or not a Circle.

Finally, note that your class should not expose any of its internal state publicly.

Anonymous Classes
Anonymous Classes

We’ve seen how to define what is called a named class. This should be familiar to us by now:

// Person is a named class
public class Person { }

However, Java also allows us to define so-called anonymous classes. Let’s go through an example carefully:

public class Person {
public String getType() {
return "Person";
}
}
Person person = new Person();
Person student = new Person() {
@Override
public String getType() {
return "Student";
}
};
System.out.println(person.getType());
System.out.println(student.getType());

In contrast to named classes, anonymous classes:

Capturing Variables
Capturing Variables

Anonymous classes can capture the value of variables that are available when they are created. This can be extremely useful. However, there are limitations to this approach. Most importantly, the variables that are captured must either be final or effectively final.

Let’s look at an example:

public class Person {
public String getType() {
return "Person";
}
}
// Show variable capture

Uses for Anonymous Classes
Uses for Anonymous Classes

We can create anonymous classes in Java. Cool! But… so what!? What problems can these classes solve?

Surprisingly, anonymous classes turn out to be common and quite powerful. Let’s look at an example.

Imagine that we want to count the number of elements in an int array that meet some condition. The condition could be that the element was positive, or negative, or odd, or even, or divisible by three, or whatever.

One approach would be to write separate methods for each thing we would want to count:

int countArrayPositive(int[] values) {
int count = 0;
for (int value : values) {
if (value >= 0) {
count++;
}
}
return count;
}
int countArrayNegative(int[] values) {
int count = 0;
for (int value : values) {
if (value < 0) {
count++;
}
}
return count;
}
int countArrayEven(int[] values) {
int count = 0;
for (int value : values) {
if (value % 2 == 0) {
count++;
}

Wow, this is getting tedious—and we’ve only done three! Imagine if we had a bunch of different conditions we needed to handle… But they are all very similar. There must be a better way.

Let’s see how to rewrite the code above using an anonymous class in a way that makes the counting logic completely flexible.

int countArrayPositive(int[] values) {
int count = 0;
for (int value : values) {
if (value >= 0) {
count++;
}
}
return count;
}
int countArrayNegative(int[] values) {
int count = 0;
for (int value : values) {
if (value < 0) {
count++;
}
}
return count;
}
System.out.println(countArrayPositive(array));

Practice: Bracket an Anonymous Class

Created By: Geoffrey Challen
/ Version: 2020.10.0

Declare a public class Bracketer providing one static method create. create takes a single int parameter and returns an anonymous object that implements the Bracket interface:

The returned object should implement top so that it returns the passed int and bottom so that it returns the passed int * -1. So, for example:

Homework: String Both Ways Anonymous Class

Created By: Geoffrey Challen
/ Version: 2021.10.0

Declare a public class BothWays providing one static method create. create takes a single String parameter and returns an anonymous object that implements the IBothWays interface:

So, for example:

If the String passed to create is null, throw an IllegalArgumentException.

CS People: Shoshana Zuboff
CS People: Shoshana Zuboff

Shoshana Zuboff is an author and professor at the Harvard Business School, where she was the first tenured female professor. She may be best known for coining the term “surveillance capitalism”, and for her book “The Age of Surveillance Capitalism”, which outlines a global system of behavioral modification based on the collection and mining of data about our private lives.

In the short video below Shoshana Zuboff defines surveillance capitalism and discusses how it mirrors and differs from previous eras of capitalism:

More Practice

Need more practice? Head over to the practice page.