-
Notifications
You must be signed in to change notification settings - Fork 55
/
Access Modifiers in Java
111 lines (75 loc) · 4.06 KB
/
Access Modifiers in Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Access Modifiers
Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N
Source of the above table: http://javapapers.com/core-java/access-modifiers-in-java-explain/
Access level of class:
Java class has mainly two type of access level:
default: class objects are accessible only inside the package.
public: class objects are accessible in code in any package.
private: can only be accessed within that class. Not anywhere outside it
protected: Can be accessed within the class and all the classes which inherit this class and only
within that package.
Source: http://staff.science.uva.nl/~heck/JAVAcourse/ch4/ss2_2.html
http://javapapers.com/core-java/access-modifiers-in-java-explain/
Access Specifiers
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of
data in a class and making this class available only through methods. In this way the chance of
making accidental mistakes in changing values is minimized. Java allows you to control access to
classes, methods, and fields via so-called access specifiers.
Java offers four access specifiers, listed below in decreasing accessibility:
public
protected
default (no specifier)
private
We look at these access specifiers in more detail.
public
public classes, methods, and fields can be accessed from everywhere.
The only constraint is that a file with Java source code can only contain one
public class whose name must also match with the filename. If it exists, this
public class represents the application or the applet, in which case the public
keyword is necessary to enable your Web browser or appletviewer to show the applet.
You use public classes, methods, or fields only if you explicitly want to offer access
to these entities and if this access cannot do any harm. An example of a square determined
by the position of its upper-left corner and its size:
public class Square { // public class
public x, y, size; // public instance variables
}
protected
protected methods and fields can only be accessed within the same class to which the methods and
fields belong, within its subclasses, and within classes of the same package, but not from anywhere
else. You use the protected access level when it is appropriate for a class's subclasses to have access
to the method or field, but not for unrelated classes.
default (no specifier)
If you do not set access to specific level, then such a class, method, or field will be accessible
from inside the same package to which the class, method, or field belongs, but not from outside this
package. This access-level is convenient if you are creating packages. For example, a geometry package
that contains Square and Tiling classes, may be easier and cleaner to implement if the coordinates of
the upper-left corner of a Square are directly available to the Tiling class but not outside the
geometry package.
private
private methods and fields can only be accessed within the same class to which the methods and
fields belong. private methods and fields are not visible within subclasses and are not inherited
by subclasses. So, the private access specifier is opposite to the public access specifier.
It is mostly used for encapsulation: data are hidden within the class and accessor methods are
provided. An example, in which the position of the upper-left corner of a square can be set or
obtained by accessor methods, but individual coordinates are not accessible to the user.
public class Square { // public class
private double x, y // private (encapsulated) instance variables
public setCorner(int x, int y) { // setting values of private fields
this.x = x;
this.y = y;
}
public getCorner() { // setting values of private fields
return Point(x, y);
}
}
Summary of Access Specifiers
Access Modifiers
Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N