Cloneable

T

Thomas Weidenfeller

Roedy said:
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
 
S

Stefan Schulz

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

Thomas G. Marshall

Thomas Weidenfeller coughed up:
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---------
 
T

Thomas G. Marshall

Thomas G. Marshall coughed up:
Thomas Weidenfeller coughed up:


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

Hemal Pandya

Thomas said:
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
 
R

Roedy Green

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
 
T

Thomas G. Marshall

Roedy Green coughed up:
On Fri, 15 Jul 2005 16:13:51 GMT, "Thomas G. Marshall"



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ColorChooser for AWT 3
missing system properties 1
ConcurrentModificationException, please help 3
Splitting a class 5
Dialogs in Applets 9
for:each bummer 5
fast ant 4
compiling a source tree 6

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top