 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Madhur Ahuja Guest
|
Posted: Mon Sep 20, 2004 12:41 pm Post subject: attempting to assign weaker access privileges |
|
|
Hello
This error normally occurs when a subclass overrides a method
but doesnt preserves the original access type like public.
However I am not able to understand what is the meaning of this
error especially what it means when it says *weaker*.
--
Madhur Ahuja [madhur<underscore>ahuja<at>yahoo<dot>com]
Homepage & other stuff
http://madhur.netfirms.com
|
|
| Back to top |
|
 |
Paul Lutus Guest
|
Posted: Mon Sep 20, 2004 6:16 pm Post subject: Re: attempting to assign weaker access privileges |
|
|
Madhur Ahuja wrote:
| Quote: | Hello
This error normally occurs when a subclass overrides a method
but doesnt preserves the original access type like public.
However I am not able to understand what is the meaning of this
error especially what it means when it says *weaker*.
|
If you have a method in a parent class with a given access, and if you
override it while also giving it less general (example: private) access,
this error will result. This restriction exists because if access were
freely modifiable in subclasses, the entire idea of access control would
have no meaning - one could override the intent of the class designer along
with the methods.
http://www.janeg.ca/scjp/overload/overridingMethods.html
--
Paul Lutus
http://www.arachnoid.com
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Mon Sep 20, 2004 9:16 pm Post subject: Re: attempting to assign weaker access privileges |
|
|
On Mon, 20 Sep 2004 11:16:59 -0700, Paul Lutus <nospam (AT) nosite (DOT) zzz> wrote:
| Quote: | Madhur Ahuja wrote:
Hello
This error normally occurs when a subclass overrides a method
but doesnt preserves the original access type like public.
However I am not able to understand what is the meaning of this
error especially what it means when it says *weaker*.
If you have a method in a parent class with a given access, and if you
override it while also giving it less general (example: private) access,
this error will result. This restriction exists because if access were
freely modifiable in subclasses, the entire idea of access control would
have no meaning - one could override the intent of the class designer
along with the methods.
|
This is not actually a problem as long as you "widen" access. (e.g. make a
protected method public in a subclass). There are a few things you can not
do, however:
* publish private methods. Easy, since you can not access them from your
subclass. You _can_ however create a public method with the same signature.
* Narrow access. You can not make a public method protected in the
subclass.
Otherwise, the concept of Object Polymorphism would plainly not work. (An
instance of a subclass could not be assumed to have all the public methods
of the superclass)
* (For some strange reason) Modify return values. Yes, even if the new
Return value is a subclass of the old one, you must keep return types
equal. The rationale for this is not really apparent to me, but that's the
way it is
See you
Stefan
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|