Skip to content

Commit 8bba7fa

Browse files
committed
Merge pull request rampatra#1 from rajajain/patch-1
fixed typo and added member method in parent class to show behavior of 'protected' access modifier
2 parents 80509e6 + 3c2513c commit 8bba7fa

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

_posts/2015-05-22-access-control.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Two __types of access__ are there:
1616
* Whether a subclass can inherit a member of its superclass
1717

1818
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)_.
2120

2221
You cannot access a protected member using the dot (.) operator in the
2322
subclass **if the subclass is in a different package** from the parent class.
@@ -28,6 +27,10 @@ The following code snippet makes it clear:
2827
package certification;
2928
public class Parent {
3029
protected int x = 9; // protected access
30+
31+
protected void printMessage(String message){
32+
System.out.println(message);
33+
}
3134
}
3235

3336
package other; // different package
@@ -40,6 +43,8 @@ class Child extends Parent {
4043
// p reference?
4144
System.out.println("X in parent is " + p.x); // Compiler
4245
// error
46+
System.out.println(p.printMessage("Calling Protected method of Parent class, Compiler should throw error."));//Compiler
47+
// error
4348
}
4449
}
4550
{% endhighlight %}

0 commit comments

Comments
 (0)