In java their are 4 access modifiers public,protected,<default>,<package>,private

  • Public-PublicĀ  is the access modifiers which shows the highest visibility any class member declare as public can be visible
    1.Within the class.
    2.Within the package.
    3.Outside the package.
  • Protected-Any class member declare as protected are visible
    1.Within the class.
    2.Within the package.
    3.Out side the package but only in the case of is-a relationship.
  • Default-
    If we do not any access modifier to declare then the class member is consider as default member.
    1.Default member are visible within the class
    2.Within the package.
  • Private-
    Private is the access modifier which shows the least visibility if we declare any member as private then it is visible
    1.Only within that classes.

Understanding all java access modifiers

Let’s understand the access modifiers by a simple table.

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYNNN
DefaultYYNN
ProtectedYYYN
PublicYYYY

If you have any further clarification on Access Modifiers in Java ,How Access Level Helps in Inheritance and Polymorphism please give comment bellow

Leave a Reply

Your email address will not be published. Required fields are marked *