Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi!
I have some problem with JAVA.I read many java books but I still have problem
with something like(private ,public protected..).
Can you tell me how Privat, public.. are working, please help me.
Tanks
Peter

public variables can be accessed directly from another class:
class Test
{
public int x;
}class Main
{
Test mytest = new Test();
mytest.x=10;//this is legal
}private variables can not.
class Test
{
private int x;
}class Main
{
Test mytest = new Test();
mytest.x=10;//this is an error
}Hope this helps,
-SN

public class Parent{
public int myPublic;
protected int myProtected;
private int myPrivate; //=)
//stuff like constructors and methods
}
public class NotChild{
Parent myParent = new Parent();
//this will always work
int parentPublic = myParent.myPublick;
//this wont work
int parentProtected = myParent.myProtected;
//this will never work
int parentPrivate = myParent.myPrivate;
}
public class YesChild extends Parent{
//this will always work
int parentPublic = myParent.myPublick;
//this work because it can see protected variables
int parentProtected = myParent.myProtected;
//this will never work
int parentPrivate = myParent.myPrivate;
}public - anyone can see and/or modify
protected - only child classes can see
private - nothing outside the class can use that variable or methodhope that helps

![]() |
array input problem in c+...
|
Domino Designer
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |