Access specifier’s

Posted on

      A class provides us with two access specifier’s namely

  1. Private.
  2. Public.

        Members declared as “private” in a class cannot be accessed from out side the class. Then can be accessed only through “public”. Members of the main class. On the other hand, public members can be accessed from outside the class with an object. By default if you do not specify the access specifier’s, the default is taken to be public.

class Box
{
private double width;
private double height;
private double depth;
private void input()
{
width=10;
height=20;
depth=30;
}
void compute()
{
input();
double vol=width*height*depth;
System.out.println("The volume of the box is : "+vol);
}
}
class App21
{
public static void main(String yck[])
{
Box b1=new Box();
b1.compute();
}
}


output:

\>javac App21.java
\>java App21
The volume of the box is : 6000.0

Leave a Reply

Your email address will not be published. Required fields are marked *