We help IT Professionals succeed at work.

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

Rohit Bajaj
Rohit Bajaj asked
on
2,043 Views
Last Modified: 2017-03-29
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
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT

Commented:
Seemed like a reasonable answer...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions