Java Access Modifiers
Access Modifiers
This is how we create security by knowing who accesses a Class, Variable or Method .
Types Of Access Modifiers
1)Public
2)Protected
3)Default
4)Private
2)Protected
3)Default
4)Private
Public
Any class can be access from anywhere.
Protected
It is visible to the class it is in and its subclasses and to others in the package it is in.
Default
Only the class you are in and the people in the package you are in can see.
Private
Only people in their class can see it.
Example👇
- Here class B is Inherited from class A.
- Here class D is Inherited from class C.
- Here, if class A is Private, other classes and packages cannot access it.
- Here , if class A is Default, B and C classes can access it.(Same package)
- Here , if class C is Default, they are visible(can access) to class C,B and A.
can't visible(can't access) to class D. (Different package)
- If things in class C are Protected, they are visible to classes C,A and B.
class D is also visible(can access).(Because a subclass and but different package) - If the things in class A are Public, they are visible(can access) to classes A,B,C,E and D.
Comments
Post a Comment