Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessibility of overriden method.

 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a method is overridden in the the sub class, the overriding
methods accessibility should not be more restrictive than overridden
method,i.e public method in the super class can't be overriden to
private in the sub class. When this restriction is imposed in Java.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See 8.4.6 Inheritance, Overriding, and Hiding in Java Language Specification.
Eugene.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rationale behind this all is, that if a class B extends a class A and it overrides a method m, an object of class B 'is a' A. If an object of class A publishes method m as, say, public, an object of class B has to keep this promiss: any overridden method m must be public, otherwise class B would break the contract of 'being an A'.
kind regards
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jos Horsmeier:
The rationale behind this all is, that if a class B extends a class A and it overrides a method m, an object of class B 'is a' A.

Beware that "is a" isn't very well defined.
A more formal explanation is the Liskov Substitution Principle (LSP): objects of type B should be substitutable for objects of type A.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic