Re: How is tag interface functionality implemented in Java continued
...?
On Sep 7, 4:55 pm, Lew <no...@lewscanon.com> wrote:
ankur wrote:
//MyTestClass.java
package Pack1;
public class MyTestClass implements Cloneable{
private int g = 9;
public void meth(){
System.out.println(g);
Go easy on the indentation for Usenet!
Two spaces, or if you must, up to four per indent suffice.
}
public Object clone()
{
try{
return super.clone();
}
catch ( Exception e ){
return new String("There was an =
expection");
}
}
}
//Driver.java
package Pack1;
public class Driver {
/**
* @param args
*/
public static void main(String[] args) {
MyTestClass my = new MyTestClass();
Object obj = my.clone();
if (obj instanceof String)
{
System.out.println(obj);
}
else
{
MyTestClass my1 = (MyTestClass=
)obj;
my1.meth();
}
}
}
This gives "There was an exception" and 9
Actually, based on the code you posted, it would have given an Exception =
with
the message "There was an expection".
Not true. The code as posted would give 9.
if I delete and keep the "implements cloneable interface"
You mean "implements Cloneable". Spelling counts, as previously mentio=
ned.
Of course, the word "interface" does not appear at that point in the code=
..
Yes, sorry. I meant "implements Cloneable"
in the class declaration
public class MyTestClass implements Cloneable
My question is how does this work ??
As previously explained, and is mentioned in the Javadocs, the call to
super.clone() eventually invokes Object#clone(), which does the check.
If you look at Object.java and Cloneable.java there is not code level
connection !!!
Read the Javadocs.
How is the throwing of exception implemented !!!
Got enough punctuation marks at the end of your sentences?
This question was answered when you asked it four hours earlier. The
super.clone() call invokes the Object.clone() method, which throws an
exception if 'getClass()' is not a subtype of Cloneable.
This is explained in the Javadocs for the Object#clone() method:
<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()>
Read the Javadocs and the question is still not answered.
"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. "
How is the exception thrown? I am trying to understand by looking at
Java library code how the exception is thrown ??
I did not(could not) find anything to that effect in Object.java,
Cloneable.java, Exception.java etc !!
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.
Have you read the Javadocs? Did you read the earlier answers to this q=
uestion?
--
Lew