r3mo
r3mo

Reputation: 547

Instantiating protected class from outside package

I have rewritten the Binary Search Tree code found in the following links to work generically - the class will work with any type parameter. http://www.informatics.susx.ac.uk/courses/dats/DataStructures/SearchTree.java http://www.informatics.susx.ac.uk/courses/dats/teach/code/demos/SearchTreeDemo.java

I placed my generic SearchTree.java in a package named mysearchtree, and imported that into SearchTreeDemo.java.

SearchTree.java contains: Abstract class SearchTree. Concrete class EmptyTree, which has a protected constructor. Concrete class NodeTree, which also has a protected constructor.

The original, non-generic coding for the SearchTree abstract class was that a client of the code would instantiate an EmptyTree object through a static method as below:

public abstract class SearchTree {
    /**
     * Returns an empty tree.
     */
    public static SearchTree empty() {
        return new EmptyTree();
    }

    // more code
}

This would have been used as follows:

SearchTree t = SearchTree.empty();
t = t.add( new String("hello") );

When working with generics, I have re-written the class to have the following structure:

public abstract class SearchTree<T extends Comparable<T>> {
    public abstract boolean isEmpty();
    public abstract int numNodes();
    public abstract boolean contains(T key);
    public abstract SearchTree<T> add(T item);
    public abstract SearchTree<T> remove(T item);
    public abstract String toString();
}

The original plan of providing a static method that would return an EmptyTree will not work generically (you can't use the generic type parameters of the class in a static method).

My question is: How do I provide a type-safe way of allowing a client of this code to instantiate a EmptyTree from outside the package (or some other equivalent mechanism that would replace the SearchTree t = SearchTree.empty() code)?

Update:

I have tried the following, but get an error:

public static <U> SearchTree<U> createSearchTree() { return new EmptyTree<U>(); }

Errors:

./mysearchtree/SearchTree.java:7: type parameter U is not within its bound
    public static <U> SearchTree<U> createSearchTree() { return new EmptyTree<U>(); }
                                 ^
./mysearchtree/SearchTree.java:7: type parameter U is not within its bound
    public static <U> SearchTree<U> createSearchTree() { return new EmptyTree<U>(); }
                                                                              ^
2 errors

Upvotes: 1

Views: 1447

Answers (3)

ditkin
ditkin

Reputation: 7054

Is something like this what you are looking for? A parametrized method.

public static <Y extends Comparable<Y>> SearchTree<Y> empty() {
   return new SearchTree<Y>();
} 

Upvotes: 1

Thom
Thom

Reputation: 15092

Maybe pass the class in to your staic method?

Upvotes: 0

Mike Thomsen
Mike Thomsen

Reputation: 37526

If the constructors are protected, the only way I can think of would be either the singleton or factory patterns (the latter being much more logical).

Upvotes: 0

Related Questions