Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiating protected class from outside package
    primarykey
    data
    text
    <p>I have rewritten the Binary Search Tree code found in the following links to work generically - the class will work with any type parameter. <a href="http://www.informatics.susx.ac.uk/courses/dats/DataStructures/SearchTree.java" rel="nofollow">http://www.informatics.susx.ac.uk/courses/dats/DataStructures/SearchTree.java</a> http://www.informatics.susx.ac.uk/courses/dats/teach/code/demos/SearchTreeDemo.java</p> <p>I placed my generic SearchTree.java in a package named mysearchtree, and imported that into SearchTreeDemo.java.</p> <p>SearchTree.java contains: Abstract class SearchTree. Concrete class EmptyTree, which has a protected constructor. Concrete class NodeTree, which also has a protected constructor.</p> <p>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:</p> <pre><code>public abstract class SearchTree { /** * Returns an empty tree. */ public static SearchTree empty() { return new EmptyTree(); } // more code } </code></pre> <p>This would have been used as follows:</p> <pre><code>SearchTree t = SearchTree.empty(); t = t.add( new String("hello") ); </code></pre> <p>When working with generics, I have re-written the class to have the following structure:</p> <pre><code>public abstract class SearchTree&lt;T extends Comparable&lt;T&gt;&gt; { public abstract boolean isEmpty(); public abstract int numNodes(); public abstract boolean contains(T key); public abstract SearchTree&lt;T&gt; add(T item); public abstract SearchTree&lt;T&gt; remove(T item); public abstract String toString(); } </code></pre> <p>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).</p> <p>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 <code>SearchTree t = SearchTree.empty()</code> code)?</p> <p><strong>Update:</strong></p> <p>I have tried the following, but get an error:</p> <pre><code>public static &lt;U&gt; SearchTree&lt;U&gt; createSearchTree() { return new EmptyTree&lt;U&gt;(); } </code></pre> <p>Errors:</p> <pre><code>./mysearchtree/SearchTree.java:7: type parameter U is not within its bound public static &lt;U&gt; SearchTree&lt;U&gt; createSearchTree() { return new EmptyTree&lt;U&gt;(); } ^ ./mysearchtree/SearchTree.java:7: type parameter U is not within its bound public static &lt;U&gt; SearchTree&lt;U&gt; createSearchTree() { return new EmptyTree&lt;U&gt;(); } ^ 2 errors </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload