Forums Register Login

inheritance question

+Pie Number of slices to send: Send
i know that a protected instance method can be called from within any subclass. so i don't understand why the following code can't compile. shouldn't clone (a protected method ) of the object class be able to be called from the subclass?
public class Sub extends Object {
public static void main(String [] args) {
Object x = new Object();
// compiler complains that clone is protected
x.clone();
}
}
+Pie Number of slices to send: Send
Hi Roy
Welcome to javaranch. The method is protected and hence is only available to classes in the same package or subclasses. But subclasses have only the inherited method that is they can use clone as in the following code but can not access it the way you are trying to access it it. There is a very good descision on this. You will find it here.


[ June 03, 2003: Message edited by: Anupam Sinha ]
+Pie Number of slices to send: Send
Anupam, good reference there. That was a good read! I didn't originate this post, but thanks!
[ June 03, 2003: Message edited by: Brian Joseph ]
+Pie Number of slices to send: Send
good post that cleared my doubts. thanks!
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 616 times.
Similar Threads
clone method?
Shallow Copy implementation Question
Why protected method in class Object?
clone ( )
Object Cloning
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:47:17.