Cloneable  
Author Message
Roedy Green





PostPosted: 2005-7-15 16:34:00 Top

java-programmer, Cloneable 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?

--
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





PostPosted: 2005-7-15 16:54:00 Top

java-programmer >> Cloneable Roedy Green wrote:
> 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

--
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/
 
Stefan Schulz





PostPosted: 2005-7-15 16:54:00 Top

java-programmer >> Cloneable On Fri, 15 Jul 2005 08:33:52 +0000, Roedy Green wrote:

> 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.

--
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"


 
 
Thomas G. Marshall





PostPosted: 2005-7-16 0:12:00 Top

java-programmer >> Cloneable Thomas Weidenfeller coughed up:
> Roedy Green wrote:
>> 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


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---------



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


 
 
Thomas G. Marshall





PostPosted: 2005-7-16 0:14:00 Top

java-programmer >> Cloneable Thomas G. Marshall coughed up:
> Thomas Weidenfeller coughed up:
>> Roedy Green wrote:
>>> 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
>
>
> 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()

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



> {
> 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---------



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


 
 
Hemal Pandya





PostPosted: 2005-7-16 1:28:00 Top

java-programmer >> Cloneable Thomas G. Marshall wrote:
> Thomas Weidenfeller coughed up:
> > Roedy Green wrote:
>
> 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

 
 
Roedy Green





PostPosted: 2005-7-16 18:28:00 Top

java-programmer >> Cloneable On Fri, 15 Jul 2005 16:13:51 GMT, "Thomas G. Marshall"
<email***@***.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.

--
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





PostPosted: 2005-7-16 23:58:00 Top

java-programmer >> Cloneable Roedy Green coughed up:
> On Fri, 15 Jul 2005 16:13:51 GMT, "Thomas G. Marshall"
> <email***@***.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.

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

--
Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....