Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance question

 
roy john
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
roy john
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good post that cleared my doubts. thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic