Java: inherits a generic type

Possible duplicate:
Extension from a common supertype?

Hello everybody. I came from C ++ and I loved working with templates. I would like, in Java, to β€œinsert” a node into the inheritance tree at a specific point. ... I think the code will be more explicit:
public class MyClass<E> extends E {}

This works fine with C ++ templates, but java gives me "cannot refer to type of type as supertype". Is there any way to do this in java?

Thank you for your help;)

+3
source share
1 answer

To understand the problem with the code you tried, consider what would happen if you Ewere, say Integer,?

You can do what you want with nodes that contain common data:

public class Node<T> {
    T data;
    // other tree bookkeeping fields
}

Then you can define subclasses:

public class ThreadedTreeNode<T> extends Node<T> {
    // even more bookkeeping fields
}
+3

All Articles