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
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Because the Object class does not support the Cloneable interface.
0
dpearsonCommented:
I'm not really sure what code you are writing that causes that error.

This works for me:

public class ClassTest {
	public static void main(String[] args) throws CloneNotSupportedException {
		ClassTest t = new ClassTest() ;
		t.clone();
	}
}

Open in new window


and throws the CloneNotSupportedException when I call it - which is the default for Object (as krakatoa pointed out).

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
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

0
krakatoaCommented:
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

0
dpearsonCommented:
Seemed like a reasonable answer...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.