0

I'm using the Idiom from this Discussion How can I make an interface instance method accept arguments of the same class only, really?:

interface ITree<SELF extends ITree<SELF>>{
    SELF getNode(int index);

    void setNode(int index, SELF node);
}

My question is, how can I implement the Tree which extends ITree in a right why? My current code looks like this:

public class B {
private ITree tree;
    public B (ITree tree){
        this.tree = tree;
    }
}

As expected it throws warnings that I should add a Type. I want to let the class which uses B decide which Type it should use. Or better I don't want to use generics at all for such a minor Problem.

Edit:

I found the solution after evaluating the answers. The devils detail was in this line:

<T extends ITree<T>>

Here is the full source code:

public class B<T extends ITree<T>> {
private T tree;
    public B (T tree){
        this.tree = tree;
    }
}
Community
  • 1
  • 1
Durrahan
  • 45
  • 5

1 Answers1

2
public class B<T extends ITree> {
private T tree;
    public B (T tree){
        this.tree = tree;
    }
}
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • Still this doesn't change the fact that ITree needs a Type of the same Interface. Which still generates the same warnings. "ITree is a raw type. References to generic type ITree should be parameterized." – Durrahan Jun 09 '14 at 11:29