David
David

Reputation: 14963

java cloneing error

I have a method that tries to clone an instance of a class (B) i have writen.This method is being called in class A so this method is not in the same class as the class i'm trying to clone (B). The class i'm trying to clone impliments colneable. when i try to compile my code i get an error message that reads "clone() has protected access in Java.lang.Object. Why might this be happing?

Upvotes: 1

Views: 394

Answers (3)

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

If you want some code inside class A to clone an instance of class B, then either place A and B in the same package, or broaden the access to B.clone() by making it public (rather than leaving it protected-scope.)

Furthermore, I would refer you to the book Effective Java by Josh Bloch. I found a PDF of Chapter three here

Upvotes: 1

nairdaen
nairdaen

Reputation: 1047

That may be happening because you are trying to call the clone() method outside its allowed access. For you to be able to call it, the class that is calling it should extend directly from Object, or belong to the Same Package, or be Object. More information here http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Upvotes: 1

Umesh Kacha
Umesh Kacha

Reputation: 13686

You can only access protected members of a type in a different package if the compile-time type of the expression you're referencing it through is either your own class or a subclass.

Check this link.

Upvotes: 0

Related Questions