File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected" Watch "protected" New topic


amrit singh
Ranch Hand

Joined: Nov 03, 2000
Posts: 42
package points;
class Point{
protected int x,y;
what does following lines means:
The fields x and y are declared protected and are accessible outside the package points only in subclasses of class
Point, and only when they are fields of objects that are being implemented by the code that is accessing them.
pls explain protected access moifier
when applied to methods,fields and constructor
sunil choudhary
Ranch Hand

Joined: Nov 10, 2000
Posts: 141
Hi Amrit look at the following code
compile it using
javac points\*.java
and then run it by
java points.fultu
(remember to put the whole file in a new dir called points
if you make another class in some other package and try to access variables x and y you will not be able to do that.
however inside the class or to the classses which extend this class the variables are available .
YOu can look at any book for more examples.I am sure there are plenty of them.

package points;
class Point{
protected int x=1,y=2;
class fultu extends Point{
public static void main(String args[]){
Point p=new Point();

"Learning is weightless, a treasure you can always carry easily." -Chinese Proverb
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Amrit,
Take a look at this earlier discussion
Hope it helps.

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: protected
It's not a secret anymore!