开发者

what could this generic class declaration could mean?

开发者 https://www.devze.com 2023-04-06 22:41 出处:网络
I know this isn\'t a good question to ask and I might get cursed to ask it but I cannot find any place to get help on this question

I know this isn't a good question to ask and I might get cursed to ask it but I cannot find any place to get help on this question

Below is a Generic class that appeared in my interview question (which I h开发者_如何转开发ave already failed). The question was to tell what this Class declaration is doing and in what circumstances this could be used for ?

I have very limited understanding of Generic programming but I understand that 'T' is Type and 'extends' here means that the Type should have inherited 'SimpleGenericClass' but I do not understand the '?' at the end and in what circumstances this Class could be potentially used for

public abstract class SimpleGenericClass<T extends SimpleGenericClass<?>> {

}


First, because the class SimpleGenericClass is abstract, it is meant to be subclassed.

Second, it is a generic class which means that inside the class somewhere you will almost assuredly be using the generic parameter T as the type of a field.

public abstract class SimpleGenericClass<T...> {
    T x;
}

Now the first interesting thing here is that T is bounded. Because it is declared as T extends SimpleGenericClass<?> it can only be SimpleGenericClass<?> or some subclass of SimpleGenericClass<?>. You also asked about thr ?. That's known as a wildcard and there is a pretty good explanation of it at the Java Tutorial on Wildcards. In your case we would say this is a "SimpleGenericClass of unknown." It is needed in Java because SimpleGenericClass<Object> is NOT the superclass of SimpleGenericClass<String>, for example.

The second interesting thing though is that since T is a SimpleGenericClass of some sort, your class is more than likely defining recursive structures. What comes to my mind are trees (think of expression trees) where SimpleGenericClass is the (abstract) node type, designed to be subclassed with all kinds of specialized node types.

UPDATE This SO question on self-bounded generics might be helpful to you.

UPDATE 2

I went ahead and put together some code that illustrates how this can be used. The app doesn't do anything but it does compile and it shows you how the generic bounds can supply some possibly-meaningful constraints.

public abstract class Node<T extends Node<?>> {
    public abstract T[] getChildren();
}

class NumberNode extends Node {
    int data;
    public Node[] getChildren() {return new Node[]{};}
}

class IdentifierNode extends Node {
    int data;
    public Node[] getChildren() {return new Node[]{};}
}

class PlusNode extends Node {
    NumberNode left;
    NumberNode right;
    public NumberNode[] getChildren() {return new NumberNode[]{};}
}

The nice thing here is that NumberNode[] is a valid return type for PlusNode.getChildren! Does that matter in practice? No idea, but it is pretty cool. :)

It's not the greatest example, but the question was rather open ended ("what might such a thing be used for?"). There are other ways to define trees, of course.


This really only means that you allow the user of class SimpleGenericClass to parametrize instances of the class with the type T. However, T cannot be any type, but must be a subtype of SampleGenericClass (or SampleGenericClass itself).

In the remainder of the code of class SimpleGenericClass you may use type T in method signatures.

Let's assume for a second that SimpleGenericClass is not abstract. When using it, you could then write:

new SimpleGenericClass<SampleGenericClass<String>>();

I.e. you parametrize SimpleGenericClass with SampleGenericClass and SampleGenericClass with String.


This basically sais: in this class you have a Type placeholder called T, and a restriction on that placeholder, it must be of type SimpleGenericClass or something that extends it. Once you obey that rule you can create instances of your class and give an actual type to T, that later on can be used in methods of that class, something like this:

public class C <T extends Number>{

    public void doSomething(T t) {

    }

    public static void main(String... args) {
        //works:
        C<Number> c = new C<Number>();
        c.doSomething(new Number() {
            //Aonimous implementation of number

        });

        //won't work
        //C<Object> c = new C<Object>();

        C<Integer> c2 = new C<Integer>();       
        c2.doSomething(new Integer(1));
        //won't work
        //c2.doSomething(new Number() {

            //Aonimous implementation of number
        //});
    }
}

The SimpleGenericClass<?> is pretty redundant at this point. If another generic type is needed on this class, you can have more than one (SimpleGenericClass<T extends SimpleGenericClass, T2 extends Whatever>)


By definition it says that the SimpleGenericClass can work on a type <T> which is subclass of SimpleGenericClass.

So I assume there will be some operations which will work on <T>.

Now to see why one would define a template like this - (not much I can think of , really ) may be a scenario where the SimpleGenericClass is an abstract class (just realized it is as per OP :P) and expects that it can work on any concrete classes ?

Guys what do you think ?


I guess you have got the question in this form (T instead of ?):

public abstract class SimpleGenericClass<T extends SimpleGenericClass<T>>

Take a look at this code:

abstract class Foo<SubClassOfFoo extends Foo<SubClassOfFoo>>
{
    /** subclasses are forced to return themselves from this method */
    public abstract SubClassOfFoo subclassAwareDeepCopy();
}

class Bar extends Foo<Bar> {
    public Bar subclassAwareDeepCopy() {
        Bar b = new Bar();
        // ...
        return b;
    }
}

Bar b = new Bar();
Foo<Bar> f = b;
Bar b2 = b.subclassAwareDeepCopy();
Bar b3 = f.subclassAwareDeepCopy(); // no need to cast, return type is Bar

The trick going on with Foo<SubClassOfFoo extends Foo<SubClassOfFoo>> is:

  • Any subclass of Foo must supply a type argument to Foo.
  • That type argument must actually be a subclass of Foo.
  • Subclasses of Foo (like Bar) follow the idiom that the type argument they supply to Foo is themselves.

  • Foo has a method that returns SubClassOfFoo. Combined with the above idiom, this allows Foo to formulate a contract that says “any subclass of me must implement subclassAwareDeepCopy() and they must declare that it returns that actual subclass“.

To say that another way: this idiom allows a superclass (such as an Abstract Factory) to define methods whose argument types and return types are in terms of the subclass type, not the superclass type.

The trick is done for example in Enum JDK class:

public abstract class Enum<E extends Enum<E>>

Refer here for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消