File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

protected

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
thanxxxxxx
sunil choudhary
Ranch Hand

Joined: Nov 10, 2000
Posts: 141



http://www.javaranch.com/ubb/Forum24/HTML/007189.html
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;
System.out.println(x);
System.out.println(y);
}
class fultu extends Point{
public static void main(String args[]){
Point p=new Point();
System.out.println(p.x);
System.out.println(p.y);
}
}
HTH
Sunil
-------------


"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 http://www.javaranch.com/ubb/Forum24/HTML/005221.html.
Hope it helps.
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected