Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag 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
SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Avatar of dpearson
dpearson

Seemed like a reasonable answer...