• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cloning

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Consider the following code:



Eclipse points out the compilation error:

The method clone() from the type Object is not visible


Considering that the clone() method comes from the Object class, which is inherited by all classes (so also by A and B) and that clone() is protected, why it is not visible??
[ April 21, 2008: Message edited by: Ismael Upright ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and that clone() is protected, why it is not visible??


Can you give us a definition of a protected method ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The Clone() method has to be overriden in the class A to make it accesible.
Just verify the Clonable interface to get a better idea
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ismael Upright,
First lets set the definition of 'protected' notion:
reference to K&B

The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.



1- through inheritance means that you can use the protected member/method like any other member/method inside the subclass.BUT it not allowed to use the (.)dot operator to access to this member/method outside the subclass in other package.

So in your example you have two different packages, the java.lang where the Object class is implemented and yourpackage, then the rest way to access to clone is through inheritance, as i say above you cannot use a reference to access to the clone method.

If you want to use clone with reference of B you should make this change
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All classes inherit from Object class. Which means that all classes should be able to use protected methods from class Object (because protected methods are usable in subclasses, even if they are in other packages). That's why I think that in this case we should be able to use clone().

But we cannot. Why?

I tried to create this class in java.lang, but still the clone() method is unable to use..
[ April 22, 2008: Message edited by: Ismael Upright ]
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A does inherit from Object, so it can access Object's protected methods.

The key is to remember that clone() is defined in java.lang.Object, and that's the package that's used in determining protected accessibility.

B and TestClass are not in the same package as Object, so they cannot see the protected java.lang.Object.clone() member that is inherited by A through extending Object.

To test this, add a protected override of clone to A, and see that TestClass can see it through B.

Actually, per standard implementation of Cloneable, you should add a public override of clone(), but this is just for understanding.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understand now. We cannot use clone() because in this case the TestClass is in another package than java.lang, and therefore it cannot see the protected method of the class B, right?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not yet Ismael

Please read again the following statement:

Originally posted by Stevi Deter:

B and TestClass are not in the same package as Object, so they cannot see the protected java.lang.Object.clone() member that is inherited by A through extending Object.



Horri quoted a sentence from K&B but dispite I find that book marvellous, I think it's not enough clear on this point. Try to read: whereas a protected member can be accessed (through inheritance) by a subclass as whereas a protected member can be accessed (through DIRECT inheritance) by ITS FIRST subclass (To be honest, they point this clearly out some lines after)

Moreover, if you read the last sentence well, you can see that protected members are just defined as VISIBLE and not as INHERITED as most think.

In your hierarchy, A comes right after Object. All the subsequent subclasses have to pass through A to reach Object members. And as long as both A, B and TestClass are in a different package from Object, A is THE ONLY class allowed to SEE protected Object members. But still A doesn't have a clone() method. It can just use it.

IF (but only IF, and this is NOT the case) all the four classes where in the same package (A, B, TestClass and A's ancestor), you example could work.

Hope it's clearer now.
[ April 27, 2008: Message edited by: Francesco Bianchi ]
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class At implements Cloneable
{
public int i = 10;
}
class TestClass extends At
{
public int i = 20;

public Object clone()
{
try
{
System.out.println(super.clone());

return super.clone();
}
catch (Exception e)
{
return null;
}
}
}
public class B implements Cloneable
{
public static void main(String args[]) throws Exception
{
TestClass b1 = new TestClass();
//b1.clone();
System.out.println((TestClass) b1.clone());
TestClass b2 = (TestClass) b1.clone();
System.out.println(b1==b2);
}
}

o/p --

TestClass@360be0
TestClass@45a877
TestClass@1372a1a
false

its giving me false, even though i implemented the Clone

Can someone explain
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinesh Tahiliani:
System.out.println(b1==b2);

o/p --

TestClass@360be0
TestClass@45a877
TestClass@1372a1a
false

its giving me false, even though i implemented the Clone



when is "==" not equal?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If tow different objects are created.
for eg:

String s1= new String("ami");
String s2= new String("ami");
if(s1==s2)
{
System.out.print("not equal");
}
else
System.out.print("equal");
}
in above eg, though the values are same, but tow different objects are created in memory poo.

Please let me know if iam wrong
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you meant:



When comparing objects, "==" returns true if and only if the objects refer to the same object.

Does your clone refer to the same object as the original?

What do you need to do to check if two objects are equal in the object oriented, and not reference, sense?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't get you. Can you please little elaborate
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you're dealing with objects, "==" only compares the object references. If they refer to the same object, then they're "==".

So when you call:


and it prints false, it's printing the correct answer, as described in the API for Object.clone().

What you want is b1.equals(b2). But in your current code, you'll find that also returns false, because you haven't implemented equals (and hashCode) in TestClass, so it defaults to the Object implementation which, again, just compares object references.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Francesco Bianchi:
In your hierarchy, A comes right after Object. All the subsequent subclasses have to pass through A to reach Object members. And as long as both A, B and TestClass are in a different package from Object, A is THE ONLY class allowed to SEE protected Object members. But still A doesn't have a clone() method. It can just use it.



I still don't understand..

Let's leave the method clone() for a second and focus on the inheritance mechanisms.

If class X has a protected method doSomething() and we subclass this class with A, the method doSomething() is inherited and exists in class A. It can be used with instance a of class A as follows: "a.doSomething()". Right?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ismael Upright:

If class X has a protected method doSomething() and we subclass this class with A, the method doSomething() is inherited and exists in class A. It can be used with instance a of class A as follows: "a.doSomething()". Right?



This is true if access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


As an example, let's look at the following classes; first three in the same package, fourth and fifth in a different package


This gives us a good overview of where and how protected methods are visible via package and inheritance.

Let me know if this helps clarify the mystery.
[ May 01, 2008: Message edited by: Stevi Deter ]
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I think again that I understand

The problem in my first post here is not about cloning itself but about the mechanisms of inheritance.
The method clone() from the classes A and B is not accessible because these classes are in different packages than the class Object is. I could use the method clone() but only directly within the body of the classes A and B.


Is that correct thinking?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic