Page 346 - CA_Blue( J )_Class10
P. 346
14.2 ACCESS SPECIFIERS
Since all the data members and methods are encapsulated within braces, to access them, we need to use the concept
of access specifiers. An access specifier is a property that controls the accessibility of data members and member
methods within the class or outside the class. This means that the visibility of the parts can be controlled by these
access specifiers. There are mainly three types of access specifiers in Java which are private, public and protected. Let
us study them in detail.
14.2.1 The private Access Specifier
The private access specifier restricts the accessibility of data members and member methods within a class only. This
means that the data members and member methods are preceded by the access specifier private can be accessed only
within the class in which they are declared.
Program 1 To print the sum of 4 and 2.
1 import java.util.*;
2 class private_access_sp
3 {
4 private int a,b,s;
5 private void accept(int a1,int b1)
6 {
7 a=a1; b=b1;
8 }
9 private void display()
10 {
11 s=a+b;
12 System.out.println("Sum of " + a + " and " + b + " is " + s);
13 }
14 public static void main()
15 {
16 private_access_sp ob=new private_access_sp();
17 ob.accept(4,2);
18 ob.display();
19 }
20 }
You will get the following output:
Sum of 4 and 2 is 6
14.2.2 The public Access Specifier
The public access specifier allows the data members and member methods can be accessed within the class and also
outside the class.
344344 Touchpad Computer Applications-X

