Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes simple........ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple........" Watch "simple........" New topic


M Karthikeyan

Joined: Sep 20, 2000
Posts: 2
abstract class ab
abstract void add();
abstract void sub();
class ba extends ab
void add()
void sub()

class abs
public static void main(String[] args)
System.out.println("Hello World!");
ba a1= new ba();
ba a2=(ba)a1.clone();
this code gives the following error:- Can't access protected method clone in class java.lang.Object. ba is not a
subclass of the current class.
ba a2=(ba)a1.clone();
Anyone help me out. Thanx in advance.

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Hi Karthikeyan,
You are getting this message because your class is not overriding the method clone of the class Object.
If you want your class to have cloneability, you must override clone in your class and declare it public. Also your class must implement the Cloneable interface.
In your clone method you must first call super.clone().
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Notes from the documentation on Object about clone()
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown.
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
The code then becomes something like :

Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
M Karthikeyan

Joined: Sep 20, 2000
Posts: 2
Thanx guys!
Consider Paul's rocket mass heater.
subject: simple........
Similar Threads
equals and Lists
inner class within a method as abstract???
exam lab gc question
How many Objects are elligible for Gabbage Collection at Line 17?
Garbage collection