Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

question about java template

Thread view: 
Hatter Jiang - 06 Nov 2007 08:18 GMT
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/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.