Anonymous Inner Classes

One more thing: let me teach you anonymous inner classes. If you are looking at others code (like Josh's when you look at TreeMap's source code) you might see this and be confused. An anonymous inner class is a way of shorthand-ing the creation of an inner class or the implementation of an interface. The syntax looks like this:
    public Set entrySet() {
    return new Set() {
        public boolean add(Object o) {
            throw new UnsupportedOperationException();
        }
    };
}

Notice two things--first, we are sort of ``instantiating'' an interface (we could do the same with an abstract class), but instead of a semicolon we open a curly brace and begin defining its methods. The compiler will not let you compile this code unless you implement all of the methods in Set within the braces (for obvious reasons). What actually happens behind the scenes is that the compiler changes that statement into code that looks like this:

    public Set entrySet() { return new AnonymousInnerClass1(); }
public class AnonymousInnerClass1 implements Set {
    public boolean add(Object o) {
        throw new UnsupportedOperationException();
    }
}

Pretty handy, huh?

The source code for a full implementation of SortedMap using the skiplist is in the SortedMapExample.jar file in the top level cmsc420 directory in my user pages. And, you also have the TreeMap's source code) to study.

MM Hugue 2018-10-01