Article View: borland.public.jbuilder.java.language
Article #17716Cloneable problem
From: Mikael Lindkvist
Date: Mon, 05 Oct 1998 00:00
Date: Mon, 05 Oct 1998 00:00
37 lines
1768 bytes
1768 bytes
Im having a problem with the Cloneable interface that I really cant
understand.
Example 1
1. public void copy(TargetNode node) {
2. if (node instanceof Cloneable) {
3. TargetNode newNode = (TargetNode) ((Cloneable)node).clone();
-
-
TargetNode is an interface.
When I compile this code a get an error on line 3, "method clone has
protected access in class java.lang.Object". The same thing happens when
you compile something like this:
Example 2
-
Cloneable n = (Cloneable)node;
Object o = n.clone();
-
The second line generates a compile time error. I know that clone() is
protected in Object but since "node" (expample 1) and "n" (example 2) is
of type "Cloneable" I cant understand why the compiler complains when
the "Cloneable" interface declares clone() as public.
The way I see it the compiler can't tell what the datatype will be at
runtime but it knows that it should be a class that implements Cloneable
and thereby has a public clone() method implemented. So why does it use
the Object class for access checking of clone() in my expamples above
instead of just the Cloneable interface alone ?
Can anyone explain this to me ?
Mikael Lindkvist
Technical Details
Message-ID:
<36187A77.5F48829B@infodata.sema.se>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,f88a865f106561cf,start X-Google-Attributes: gidf74b3,public From: Mikael Lindkvist <mikael.lindkvist@infodata.sema.se> Subject: Cloneable problem Date: 1998/10/05 Message-ID: <36187A77.5F48829B@infodata.sema.se> X-Deja-AN: 397839252 Content-Transfer-Encoding: 7bit Organization: Another Netscape Collabra Server User Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language