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 / July 2006

Tip: Looking for answers? Try searching our database.

Overriding clone() in Java 5 (with generics)?

Thread view: 
Ulrich Hobelmann - 20 Jul 2006 16:49 GMT
Hi, is there any way I can have a typesafe clone() in Java 5?

I have a subclass of LinkedList<T>, but calling clone() causes warnings,
and typecasting to (MyList<T>) doesn't work either.

I overrode public MyList<T> clone() in my list class, but calling
super.clone() again caused the same problem, so there's no way out ;)

How do you make clone() typesafe (or typesafely call it), and compile
without complaints?  As I only made some minor modifications to
LinkedList, I basically refuse to write my own clone() (say, with addAll()).
Hendrik Maryns - 20 Jul 2006 17:00 GMT
Ulrich Hobelmann schreef:
> Hi, is there any way I can have a typesafe clone() in Java 5?
>
[quoted text clipped - 8 lines]
> LinkedList, I basically refuse to write my own clone() (say, with
> addAll()).

Unfortunately, the java.util classes did not use the covariant return
possibility in Java 5 and reimplemented clone().  The problem is
backwards compatibility: subclasses would have to be reimplemented as
well.  So you’ll have to use @SuppressWarnings("unchecked") if you want
to get rid of the warning.

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


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.