p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Java Basics (http://p2p.wrox.com/forumdisplay.php?f=199)
-   -   Access modifiers private and public (http://p2p.wrox.com/showthread.php?t=65999)

markhh February 19th, 2008 06:01 AM

Access modifiers private and public
 
Hi all, if have been reading from 2 very good sources about using access modifiers (private, public etc). Both sources directly conflict with each other.

One says
"Under no circumstances will a sub class inherit any method or instance variable if it is defined as private in a super class"

and the other source says
"Any private instance variable that has been inherited from a super class can only be accessed indirectly through public setter and getter methods in the super class. If the setters and getters in the super class are defined as private the private instance variables in the sub class will be inaccessible."

Can anyone confirm which is correct.

Many Thanks
Mark



jomet February 19th, 2008 11:22 PM

Quote:

quote:Originally posted by markhh



One says
"Under no circumstances will a sub class inherit any method or instance variable if it is defined as private in a super class"

and the other source says
"Any private instance variable that has been inherited from a super class can only be accessed indirectly through public setter and getter methods in the super class. <s> If the setters and getters in the super class are defined as private the private instance variables in the sub class will be inaccessible."</s>


both are same
except some change in strike thorough part
read as 'the super class will be inaccessible'.

if private then variables will be inaccessible in sub class.
for accessing private variables we declare public getters and setters.
Usually by using public methods we will access private variables in sub class.

Cheers,

jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.

jomet February 19th, 2008 11:38 PM

hi
check the link also

http://www.uni-bonn.de/~manfear/javaprotection.php


jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.

markhh February 21st, 2008 04:56 AM

Many thanks for your helpful replies,

Mark



All times are GMT -4. The time now is 05:13 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.