| Author |
How to access a protected method.
|
Sumeet Anand
Greenhorn
Joined: Nov 16, 2002
Posts: 16
|
|
Hi All, I have an object which i want to clone.But when i call the clone method on that object it gives me a compile time error " clone() has protected access in java.lang.Object". Can u please tell me where i am wrong. I am attaching the code lines. DPJTable clone_pendingTransactionsDPJTable = (DPJTable)pendingTransactionsDPJTable.clone(); Kindly suggest a way out. Sumeet Chopra.
|
 |
Mani Ram
Ranch Hand
Joined: Mar 11, 2002
Posts: 1140
|
|
|
Override the clone() method with your own clone method and call super.clone() from your method. This will solve the problem.
|
Mani
Quaerendo Invenietis
|
 |
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
|
|
Java Greenhorn Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy. Thanks again and we hope to see you around the ranch!!
|
Dave
|
 |
Sumeet Anand
Greenhorn
Joined: Nov 16, 2002
Posts: 16
|
|
Hi Manivannan, I wrote a custom clone() method over riding the clone method in object class. The method is as : public Object clone() throws CloneNotSupportedException { return super.clone(); } The class which has this method implements Cloneable interface.But still when i write this code it gives me the same error. DPJTable clone_pendingTransactionsDPJTable = (DPJTable)pendingTransactionsDPJTable.clone(); Please suggest a way out.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Sumeet, Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch!
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Can you post a simple example that replicates your problem?
|
 |
Sumeet Anand
Greenhorn
Joined: Nov 16, 2002
Posts: 16
|
|
Hi Dirk, I have changed my name as demanded by you. Now moving on the problem part. I have a DPJTable class defined in a separate package.It overrides the clone() method defined in Object class.The method is: public Object clone() throws CloneNotSupportedException { return super.clone(); } Now i call this method from a class in another package .This file is PendingTransactions.java. DPJTable clone_pendingTransactionsDPJTable = (DPJTable)pendingTransactionsDPJTable.clone(); But it gives me an exception saying :- clone() has protected access in java.lang.Object. Now i am unable to find out why this happening so. Could u please shed some light on this..... Thanks in advance.
|
 |
Sam Zou
Ranch Hand
Joined: Nov 18, 2002
Posts: 34
|
|
Hi, I think I may have the solution to your problem. In your function clone() that you override in you DPJTable class, you shouldn't write "return super.clone()" but return "new DPJTable()" and initialize the new DPJTable'd data members and propriety to the DPJTable you want to copy. It should look like something like this: public Object clone() throws CloneNotSupportedException { DPJTable temp = new DPJTable(); //Initialize the data of temp to the //to the value of the DPJTable you want //to clone .... temp.x = this.x; .... return temp; } By calling super.clone(), it just return an Object and not a DPJTable. I hope this may help you. Let me known. Yves
|
Sam<p>"Life is difficult"<br /> -Scott Peck-
|
 |
 |
|
|
subject: How to access a protected method.
|
|
|