aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object's method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Watch "Object New topic
Author

Object's method

kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
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.
=)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
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
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
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
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
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
April.Johnson
Ranch Hand

Joined: May 02, 2001
Posts: 48
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
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
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).
April.Johnson
Ranch Hand

Joined: May 02, 2001
Posts: 48
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
 
 
subject: Object's method