• Post Reply Bookmark Topic Watch Topic
  • New Topic

Override a method  RSS feed

 
Duc Ta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know:
Why overriding method cannot have a more restrictive access modifier than method being overriden?

Can anyone help me?
 
Dave Tolls
Ranch Hand
Posts: 2658
29
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So, what happens when the someMethod() is called.  The reference is to a MySuperClass, so the calls is valid (someMethod is public there), but the underlying object is a MySubClass...so that call should not be allowed.
Now you might say the compiler should prevent that, since it knows that msc points to a MySubClass.

However, what about:

The compiler can't figure that one out.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!