There are 4 types of java access modifiers:
- private (exclusively within the class)
- default (within in the package)
- protected (within package && outside package through inheritance)
- public (within any package)
Example of protected access modifier
In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.
Output:Hello

No comments:
Post a Comment