can i write a java class like this:
public class MyPrintJob<T extends Printable> extends T {
public void call() {
print("hello");
}
}
of course the code above cannot be compiled.
public interface Printable {
public void print(String string);
}
public class APrinter implements Printable {
public void print(String string) {
System.out.println("This is A printer:" + string);
}
}
public class BPrinter implements Printable {
System.out.println("This is B printer:" + string);
}
then when i can :
MyPrintJob<APrinter> aPrinter = new MyPrintJob<APrinter>();
aPrinter.call();
// outputs: This is A printer: hello
and :
MyPrintJob<BPrinter> bPrinter = new MyPrintJob<BPrinter>();
bPrinter.call();
// outputs: This is B printer: hello
Roedy Green - 06 Nov 2007 09:02 GMT
>public class MyPrintJob<T extends Printable> extends T {
> public void call() {
> print("hello");
> }
>}
The type erasure principle is , once generics are handled there is no
trace of them left in the object code. "extends T" would have to
have an effect on the object code, so it would have to be illegal.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 06 Nov 2007 14:23 GMT
> can i write a java class like this:
>
[quoted text clipped - 5 lines]
>
> of course the code above cannot be compiled.
What is the extra "extends T" supposed to accomplish?
Your example with APrinter and BPrinter as T doesn't show any subclass of
either APrinter or BPrinter, indeed, any relationship between the two at all.

Signature
Lew
Daniel Pitts - 06 Nov 2007 16:08 GMT
> can i write a java class like this:
>
[quoted text clipped - 5 lines]
>
> of course the code above cannot be compiled.
Templates are a C++ thing, and quite different from Java Generics.
Don't try to think of them the same way, it is not helpful. In C++
templates get expanded for every use, in Java, Generics represent a
limited meta-type system. The Generic definitions/declarations only
affect compile time type information of references. "extends T" would
have to recompile the class MyPrintJob for every MyPrintJob<x> you
created, and that simply doesn't happen.
Hope this helps,
Daniel.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>