7) Packages Lesson

Java Packages and Access Modifiers

4 min to complete · By Ryan Desmond

Both Java packages and access modifiers are used to protect the data in your programs, and knowing how to combine both will increase overall security. in your program

List of Java Package and Access Modifier Combinations

You have learned about access modifiers and how they can be used to restrict access to certain members of classes. Packages add an additional layer of access control and offer a wide range of access combinations. The following table summarizes each access modifier with its relation to package protection.


Private Member
Default Member
Protected Member
Public Member

Visible within same class

Yes Yes Yes Yes
Visible within same package by subclass No Yes Yes Yes
Visible within same package by non-subclass No Yes Yes Yes
Visible within different package by subclass No No Yes Yes
Visible within different package by non-subclass No No No Yes

If you mark a variable or method as protected, it will only be accessible by other classes within the same package.

That said, sub-classes (aka child classes) do have access to protected members even if the child class exists in another package. The default access modifier (meaning you added no access modifier - just left it blank) acts just like protected, but child classes do not have access to default members.

table showing various access modifiers in Java and their permissions

Summary: How to Use Java Packages and Java Access Modifiers?

  • Both Java packages and access modifiers protect the data in your program
  • Java packages add an additional layer of data protection to your program
  • Use the provided table to view all packages and access modifier combinations
  • protected members are only available to classes in the same package
  • Child classes always have access to protected members regardless of whether they exist in the same or different package
Morty Proxy This is a proxified and sanitized view of the page, visit original site.