Forums Register Login

Object's method

+Pie Number of slices to send: Send
class test {
public static void main(String[] args) {
Object ob = new Object();
boolean b;
Object ob2=null;
ob2 = ob.clone();
b = ob.equals(ob2);
System.out.println("Is ob equal to the clone ob2? - "+b);
}
}
The code above wont compile saying
"Can't access protected method clone in class java.lang.Object. java.lang.Object is not a subclass of the current class. ob2 = ob.clone();"
Ok so I tried to extend Object in the header of class, but still gave same error.
Why isnt this working?? Is it the access privilege that is the issue here?? The only way to compile this for me was make it explicit to the test class object but... still i dont get why above wont compile..
Also, isnt this the same thing?
class test{} and class test extends Object{}?? since every class is child of Object??
I thank you in advance.
=)
+Pie Number of slices to send: Send
You would need to put test in the same package as Object to be able to call a protected method in Object. Try:
package java.lang ;
The objects won't be equal since the equals method in Object just compares references.
Bill
+Pie Number of slices to send: Send
Hi Kevin,
This is one of those gray areas. Yes you are right in your thoughts but the API states.


The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
super.clone()


What this means is that you need another class that implements the cloneable (overrides clone method) and then your idea will work. See the following code for an example.

Regards,
Manfred.
+Pie Number of slices to send: Send
thank you all
Manfred, so by saying "implement Cloanable", means that the class B only needs to override the clone()? How come it doesnt have to declare "class B implements Cloanable" ??
thank you again for ur attention
regards
+Pie Number of slices to send: Send
I was a bit confused at first too, but what Manfred quoted from the API in effect says:
(1) The Object class DOES NOT implement the Cloneable interface.
(2) The Object class DOES implement (i.e. have) a clone() method.
This method is accessible (in the usual way) by any class "X" that directly extends Object (i.e. it doesn't extend anything else), overrides the method, and then calls super.clone().
Such a class DOES NOT have to implement the Cloneable interface itself to access the overridden method from its parent (Object). But it COULD access it even if it did implement Cloneable and define its own clone() method.
However, if the class in question extends some other class (i.e. is not a direct subclass of Object), then you will not be able to access the clone() method in Object.
Hope that helps,
Ranjan
+Pie Number of slices to send: Send
One quick note to Ranjan's comments:
"However, if the class in question extends some other class (i.e. is not a direct subclass of Object), then you will not be able to access the clone() method in Object."
This is not strictly true, although it can be. The limitation is that you can't skip implementations of overridden methods.
Consider:
class Test{
protected Object clone(Object o){
//overrode method from class Object
return super.clone(o); // calls Object.clone(Object)
}
}
class TestChild extends Test{
protected Object clone(Object o){
// overrode method from class Test
return super.clone(o); // calls Test.clone(Object)
}
}
However:
class Test2{
// no overriding of Object.clone(Object)
}
class TestChild2 extends Test2{
protected Object clone(Object o){
// overrode method from class Object
return super.clone(o); // calls Object.clone(Object)
}
}
TestChild2 is calling the clone(Object) method of Object because Test2 doesn't override that particular method.
In the first case, though, Ranjan is right in that TestChild cannot skip the clone method of Test and call Object's clone method directly.
April
+Pie Number of slices to send: Send
April, I just want to clarify a detail of your post:


return super.clone(o); // calls Object.clone(Object)


Am I correct in saying that this statement is not really calling Object.clone(), but in fact is still calling Test2.clone() -- but since Test2 did not override the clone() method it inherited from Object, the implementation of the clone() method being called is indeed defined by the Object class.
By extension, this should hold true for any number of levels down the hierarchy from Object, assuming that none of the classes in the hierarchy overrides the clone() method originally inherited from Object.
This is a bit of a fine-level technical detail, but I want to verify that you can never really have a class directly call methods from a grandparent (or higher) class using super. (But please correct me if that's an incorrect statement).
+Pie Number of slices to send: Send
Hi, Scott,
First of all, my sincere apologies to all for a typo in my previous post. The method clone() in Object does not take an Object parameter. My typing got away from me.
However, if you use the method PROPERLY as I should have typed it as in:
class Test2{
// no overriding of Object.clone()
}
class TestChild2 extends Test2{
protected Object clone(){
// overrode method from class Object
return super.clone(); // calls Object.clone()
}
}
... this will still produce the effect that I described above.
On your question about the strict technicalities of the language and whether or not you're calling a method actually from Object or just using its implementation, I do not know. I will defer to the more experienced and knowledgeable to answer that question.
Perhaps one of our moderators has an answer for us.
April
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1009 times.
Similar Threads
clone method?
Shallow Copy implementation Question
clone() method in object class
java.lang.Object
Question of the day
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:56:18.