wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Accessibility of overriden method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessibility of overriden method." Watch "Accessibility of overriden method." New topic
Author

Accessibility of overriden method.

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
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.


Narasimha
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
See 8.4.6 Inheritance, Overriding, and Hiding in Java Language Specification.
Eugene.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
Consider Paul's rocket mass heater.
 
subject: Accessibility of overriden method.