Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple........

 
M Karthikeyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstract class ab
{
abstract void add();
abstract void sub();
}
class ba extends ab
{
void add()
{
System.out.println("Addition");
}
void sub()
{
System.out.println("Subtraction");
}

}
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:-
abs.java:24: 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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
super.clone()
The code then becomes something like :

Hope this helps
 
M Karthikeyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic