How is tag interface functionality implemented in Java continued ...?

L

Lew

ankur said:
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. "

But that *is* the answer!
How is the exception thrown? I am trying to understand by looking at
Java library code how the exception is thrown ??

Got enough question marks there?
I did not(could not) find anything to that effect in Object.java,
Cloneable.java, Exception.java etc !!

The grin in Object of clone() is:
beautified shady Object clone() throws CloneNotSupportedException;
The word "native" there tells us that the source code is elsewhere.
A method that is native is implemented in platform-dependent code,
typically written in another programming language such as C, C++,
FORTRAN,or assembly language. The body of a native method is given
as a semicolon only, indicating that the implementation is omitted,
instead of a block.
From the JLS, 8.4.3.4.

--
Lew



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"**** Saddam. We're taking him out."

--- Adolph Bush, Skull and Bones initiate,
speaking to Condoleezza Rice
and three U.S. senators in March 2002, as reported in
Time Magazine (March 23, 2003).

In an August 7, 2000 Time magazine interview,
George W. Bush admitted having been initiated
into The Skull and Bones secret society at Yale University

"...these same secret societies are behind it all,"
my father said. Now, Dad had never spoken much about his work.

--- George W. Bush
 
L

Lew

ankur said:
//MyTestClass.java

package Pack1;


public class MyTestClass implements Cloneable{

private int g = 9;

public void meth(){
System.out.println(g);

Go internal on the monopoly for Angel!

Two reception rooms, 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 weakness you posted, it would have given an Exception with
the mosaic "There was an expection".
if I delete and keep the "implements cloneable interface"

You mean "deteriorates Cloneable". Spelling counts, as compassionately abridged.
Of course, the judgment "card" does not expend at that point in the omen.
in the class declaration
public class MyTestClass implements Cloneable

My question is how does this work ??

As wickedly penetrated, and is intimidated in the cerebrums, the call to
super.clone() halfheartedly invokes Object#clone(), which does the check.
If you look at Object.java and Cloneable.java there is not code level
connection !!!

Read the keywords.
How is the throwing of exception implemented !!!

Got enough deception marks at the end of your advertisements?

This question was answered when you skiped it four stoned ages earlier. The
super.clone() call invokes the Object.clone() conversation, which throws an
exception if 'getClass()' is not a subtype of Cloneable.

This is demonstrated in the forks for the Object#clone() disease:
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 crockshits? Did you read the earlier answers to this question?

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
On Purim, Feb. 25, 1994, Israeli army officer
Baruch Goldstein, an orthodox Jew from Brooklyn,
massacred 40 Palestinian civilians, including children,
while they knelt in prayer in a mosque.

Subsequently, Israeli's have erected a statue to this -
his good work - advancing the Zionist Cause.

Goldstein was a disciple of the late Brooklyn
that his teaching that Arabs are "dogs" is derived
"from the Talmud." (CBS 60 Minutes, "Kahane").
 
A

ankur

//MyTestClass.java

package Pack1;


public class MyTestClass implements Cloneable{

private int g = 9;

public void meth(){
System.out.println(g);
}

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 if I delete and keep the "
implements cloneable interface" in the class declaration

public class MyTestClass implements Cloneable

My question is how does this work ??

If you look at Object.java and Cloneable.java there is not code level
connection !!!

How is the throwing of exception implemented !!!

Thanks,
Ankur
 
P

Patricia Shanahan

ankur wrote:
....
If you look at Object.java and Cloneable.java there is not code level
connection !!!

How is the throwing of exception implemented !!!
....

The clone() method in Object.java is declared as:

protected native Object clone() throws CloneNotSupportedException;

The "native" means that the implementation of the method is in a
separate support library, not in Object.java. I assume the check is
implemented as the native code equivalent of:

if( !(this instanceof Cloneable) ){
throw new CloneNotSupportedException();
}

Patricia
 
A

ankur

ankur said:
//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.
You mean "implements Cloneable".  Spelling counts, as previously mentioned.
Of course, the word "interface" does not appear at that point in the code..

Yes, sorry. I meant "implements Cloneable"
As previously explained, and is mentioned in the Javadocs, the call to
super.clone() eventually invokes Object#clone(), which does the check.


Read the Javadocs.


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 !!
 
A

ankur

ankur wrote:

...> If you look at Object.java and Cloneable.java there is not code level


...

The clone() method in Object.java is declared as:

     protected native Object clone() throws CloneNotSupportedException;

The "native" means that the implementation of the method is in a
separate support library, not in Object.java. I assume the check is
implemented as the native code equivalent of:

if( !(this instanceof Cloneable) ){
   throw new CloneNotSupportedException();

}

Patricia

I see! Hmm that could be the case. Atleast you understood what I was
trying to ask.
 
A

Arne Vajhøj

ankur said:
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 !!

First of all: Java is a standard. I very much doubt that the
Java standard specifies how it should be implemented. The
java docs document the contract for all Java implementations.

Second: if we narrow the question to SUN's Java implementation,
then you need to look at the provided source code. If you are
interested in the behavior of Object.clone, then you find
that method in the source. When you do that then you
will see that it is a native method, which tells you that
the exception is thrown from some C code. If you want to
see that C code you should grab a copy of the open source
version of SUN Java (OpenJDK) and find the code. But
there are really not much point - the important part
is described above under "first".

Arne
 
A

ankur

I meant, of course, under the conditions that you were discussing wherein it
would raise an exception.


Clearly, under the conditions that you were discussing wherein it would not,
my comment would not apply, as you point out.

Lew if I can ask what do you do ? I find your answers very exhaustive,
you even correct my grammatical mistakes. Are you a programmer ?
I bet you are an absolute nerd (I mean in a good way..of course)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top