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 2005

Tip: Looking for answers? Try searching our database.

Cloneable

Thread view: 
Roedy Green - 15 Jul 2005 09:33 GMT
Would have been possible to some how redefine Cloneable so that clone
did not return an object but rather at object of the type implementing
clone?

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Thomas Weidenfeller - 15 Jul 2005 09:53 GMT
> Would have been possible to some how redefine Cloneable so that clone
> did not return an object but rather at object of the type implementing
> clone?

This is allowed since 1.5.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Thomas G. Marshall - 15 Jul 2005 17:11 GMT
Thomas Weidenfeller coughed up:
>> Would have been possible to some how redefine Cloneable so that clone
>> did not return an object but rather at object of the type
[quoted text clipped - 3 lines]
>
> /Thomas

This might require a little explanation.

What Thomas Weidenfeller is referring to is that when you declare your own
clone() method (as you must) you are allowed in 1.5 to specifiy a different
return type than the method that it is overriding.  In versions prior to
1.4, this would have resulted in an "incompatible return type" error

   public static class Thing implements Cloneable
   {
       public Thing clone()
       {
           try
           {
               return (Thing)super.clone();    // no can do prior to 1.5
           }
           catch (Exception ignore) { return null; }
       }
   }

---------START: 1.4 error message---------
Clone1.java:9: clone() in experiments.simple.Clone1.Thing cannot override
clone(
) in java.lang.Object; attempting to use incompatible return type
found   : experiments.simple.Clone1.Thing
required: java.lang.Object
       public Thing clone()
                    ^
---------END: 1.4 error message---------

Signature

http://www.allexperts.com is a nifty way to get an answer to just about
/anything/.

Thomas G. Marshall - 15 Jul 2005 17:13 GMT
Thomas G. Marshall coughed up:
> Thomas Weidenfeller coughed up:
>>> Would have been possible to some how redefine Cloneable so that
[quoted text clipped - 16 lines]
>    {
>        public Thing clone()

Well, for the newbies, specifically it is this line above that is the
problem, as the error message shows.

>        {
>            try
[quoted text clipped - 14 lines]
>                     ^
> ---------END: 1.4 error message---------

Signature

http://www.allexperts.com is a nifty way to get an answer to just about
/anything/.

Roedy Green - 16 Jul 2005 11:27 GMT
On Fri, 15 Jul 2005 16:13:51 GMT, "Thomas G. Marshall"
<tgm2tothe10thpower@replacetextwithnumber.hotmail.com> wrote or quoted

>Well, for the newbies, specifically it is this line above that is the
>problem, as the error message shows.

IS this something general, a method can override with a different type
so long as the type is a subtype?  The object still fulfills the
contract of the original base, but even more strictly.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Thomas G. Marshall - 16 Jul 2005 16:58 GMT
Roedy Green coughed up:
> On Fri, 15 Jul 2005 16:13:51 GMT, "Thomas G. Marshall"
> <tgm2tothe10thpower@replacetextwithnumber.hotmail.com> wrote or quoted
[quoted text clipped - 5 lines]
> so long as the type is a subtype?  The object still fulfills the
> contract of the original base, but even more strictly.

http://java.sun.com/developer/JDCTechTips/2005/tt0104.html#2

Take my example, but modify the clone to return a simple string.  Now the
clone() method is returning something that is a subtype of Object (the
return type used in Object.clone()).  Note that it does not have to be a
subtype of Thing.  This works, though is a little silly:

public class Clone2
{
   public static class Thing implements Cloneable
   {
       public String clone()
       {
           return "hello";
       }
   }

   public static void main(String[] args)
   {
       Thing thing = new Thing();
       System.out.println(thing.clone());
   }
}

So let's make a Thing and a sub-Thing (a Car):

public class Clone3
{
   public static class Thing
   {
       public List method()
       {
           return new LinkedList();
       }
   }

   public static class Car extends Thing
   {
       public ArrayList method()
       {
           return new ArrayList();
       }
   }

   public static void main(String[] args)
   {
       Car car = new Car();
       System.out.println(car.method());
   }
}

This works because the (Car).method() returns an ArrayList, a sub-type of
List.

If I modify this to return some non-sub type:

public class Clone4
{
   public static class Thing
   {
       public List method()
       {
           return new LinkedList();
       }
   }

   public static class Car extends Thing
   {
       public String method()
       {
           return "hello";
       }
   }

   public static void main(String[] args)
   {
       Car car = new Car();
       System.out.println(car.method());
   }
}

I get this (same as from 1.4) error message:

$ com Clone4.java
javac 1.5.0-beta2
Clone4.java:22: method() in experiments.simple.Clone4.Car cannot override
method
() in experiments.simple.Clone4.Thing; attempting to use incompatible return
typ
e
found   : java.lang.String
required: java.util.List
       public String method()
                     ^
1 error

Signature

Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....

Hemal  Pandya - 15 Jul 2005 18:27 GMT
> Thomas Weidenfeller coughed up:
>
> What Thomas Weidenfeller is referring to is that when you declare your own
> clone() method (as you must) you are allowed in 1.5 to specifiy a different
> return type than the method that it is overriding.

Yes, but not just any different return type. A covariant type. The
second hit on google when I search for covariance+java is from the java
glossary: http://mindprod.com/jgloss/covariance.html
Stefan Schulz - 15 Jul 2005 09:54 GMT
> Would have been possible to some how redefine Cloneable so that clone
> did not return an object but rather at object of the type implementing
> clone?

Cloneable defines no methods.

In java 1.5, you can define the clone() method to return the objects class.

Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         



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.