Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India asked on

Clone() has protected access in java.lang.Object

Hi,
I created a class dummy and created and object like :
dummy dummy = new dummy();
dummy.clone();
The above statement is giving the error : Clone() has protected access in java.lang.Object..
Why is that...  Since all classes are implicitly subclass of Object.. protected method should be accessible from dummy...
If i create a class dum2 extends dummy... and place a protected method in dummy..
then i can access dum2.protectedMethod()....
Why is that in case of dummy its not working i am unable to access the protected method of Object class which is implicitly a superclass of dummy.

Thanks
Java

Avatar of undefined
Last Comment
dpearson

8/22/2022 - Mon
SOLUTION
krakatoa

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
dpearson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
krakatoa

As Doug said, depends what you are aiming at

public class Cl{

	public class ClassTest implements Cloneable{
		protected Object clone(){return this;}
	}
	
		public static void main(String[] args) {
			ClassTest t = (new Cl()).new ClassTest() ;
			ClassTest t1 = (ClassTest)t.clone();
			System.out.println(t1 instanceof ClassTest);
			System.out.println(t1.hashCode() == t.hashCode());
		}
	
}

Open in new window

krakatoa

Mind you - I'm not quite so sure what is going on here, if you add/try :

Object o = t.clone();
			System.out.println(o instanceof ClassTest);

Open in new window

dpearson

Seemed like a reasonable answer...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck