You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2015-05-22-access-control.md
+7-2Lines changed: 7 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,8 +16,7 @@ Two __types of access__ are there:
16
16
* Whether a subclass can inherit a member of its superclass
17
17
18
18
A **default member** may be accessed only if the class accessing the member belongs to the same package,
19
-
whereas a **protected member** can be accessed by a subclass in the same package _(through dot operator and inheritance)_ and even if it is
20
-
is in a different package _(through inheritance only)_.
19
+
whereas a **protected member** can be accessed by a subclass in the same package _(through dot operator and inheritance)_ and even if it is in a different package _(through inheritance only)_.
21
20
22
21
You cannot access a protected member using the dot (.) operator in the
23
22
subclass **if the subclass is in a different package** from the parent class.
@@ -28,6 +27,10 @@ The following code snippet makes it clear:
28
27
package certification;
29
28
public class Parent {
30
29
protected int x = 9; // protected access
30
+
31
+
protected void printMessage(String message){
32
+
System.out.println(message);
33
+
}
31
34
}
32
35
33
36
package other; // different package
@@ -40,6 +43,8 @@ class Child extends Parent {
40
43
// p reference?
41
44
System.out.println("X in parent is " + p.x); // Compiler
42
45
// error
46
+
System.out.println(p.printMessage("Calling Protected method of Parent class, Compiler should throw error."));//Compiler
0 commit comments