This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cloning

 
Titu singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
protected members r allowed to access to child clas in same package or in other package
and to non-child class in same package.
clone() method is protected, but not allowed to use in other class in same package.
???
package Test;
class Test1 implements java.lang.Cloneable
{
public static void main(String [] args)throws CloneNotSupportedException
{
Test1 t1 = new Test1();
Test1 t2 = (Test1)t1.clone();
}
}

package Test;
class Test2
{
public static void main(String [] args)throws CloneNotSupportedException
{
Test1 t1 = new Test1();
Test1 t2 = (Test1) t1.clone();
}
}
compile time error : clone() has protected access in java.lang.Object
pls. write me where i am wrong.
thanks.
best regards,
titu.
[ May 23, 2003: Message edited by: Titu singh ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Titu,
Welcome to JavaRanch. Override clone() in Test1 and change the access modifier to public:
 
Titu singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Michael,
thanks for the reply.
i did the same thing even before posting this topic on the forum. that solves the problem.
but now my problem is concept behind this,
clone is protected method, inherited to Test.Test1 from Object class with protected scope.
i am not able to understand why it is not available to Test.Test2 class which is in same package.
thanks.
best regards,
titu.
 
Titu singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package Test;
class Test1 implements java.lang.Cloneable
{
public static void main(String [] args)throws CloneNotSupportedException
{
Test1 t1 = new Test1();
Test1 t2 = (Test1)t1.clone();
}
protected void doX()
{
System.out.println("in protected method");
}
}

package Test;
class Test2
{
public static void main(String [] args)throws CloneNotSupportedException
{
Test1 t1 = new Test1();
//Test1 t2 = (Test1) t1.clone();
t1.doX();
}

}
dear Michael,
both doX() and clone() r member of Test.Test1 with protected scope.
why Test.Test2 can access t1.doX() but not t1.clone();
i hope , u will be able to get my problem this time.
thanks and best regards,
titu.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why Test.Test2 can access t1.doX() but not t1.clone();
Test2 and Test1 are in the same package, so Test2 can access any package-private, public or protected methods or fields defined in any other class in the same package. Object.clone() is in a different package, so now the rules are that you can only access public methods and fields and protected methods and fields if the calling class is a subclass. Now while Test2 is a subclass of Object, you are calling clone() thru a different implementation. Here is what the JLS says:
6.6.2 Details on protected Access
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.
In your case Test1 is responsible for the implementation. Also here is an example in the JLS:
6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
  • [list]If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
  • If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
  •  
    Titu singh
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Michael,
    dumb man needs one last chance to learn this thing, pls. give me last chance.
    -------------------------------------
    Object.clone() is in a different package,
    --------------------------------------
    Test1 is child of Object , due to inheritance Test1 will have parents protected members also.
    isn't that mean, now clone() is part of Test1 class.
    isn't that mean
    Test.Test1 will has two member
    protected doX()
    protected clone()
    i know, i'm wrong somewheere. i want to learn that missing point.
    "ok, clone() is accessable to Test.Test1 with protected accessiblity, but not member of Test.Test1." is it like this.
    thanks and best regards,
    titu.
    [ May 23, 2003: Message edited by: Titu singh ]
     
    Michael Morris
    Ranch Hand
    Posts: 3451
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    dumb man needs one last chance to learn this thing, pls. give me last chance.
    Dumb men don't ask smart questions when they are trying to grasp a concept. If it takes a 100 posts, or a 1000, there are no last chances just continuing discussion.
    isn't that mean
    Test.Test1 will has two member
    protected doX()
    protected clone()
    Yes, but consider what the purpose of the protected access modifier is. Its main intent is to expose otherwise private members to its subclasses. If the runtime allowed classes of the same package to access protected members inherited from other packages by some class, then that would be a violation of encapsulation. Clearly, without exposing the protected method by overriding it, you would be going against the will of the class designer. The method is protected (or private for that matter) for a reason, so that it's state or behavior cannot be modified in some unpredictable or detrimental manner.
     
    Titu singh
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "If it takes a 100 posts, or a 1000, there are no last chances just continuing discussion."
    thanks for confidence buiding and for ur reply.
    best regards,
    titu.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic