Java Access Specifiers

Daily Debug
1 min readJan 23, 2023

--

In Java, access specifiers are used to define the level of access that a class, method, or variable has from other classes or methods. There are four types of access specifiers in Java:

public: A class, method, or variable that is declared as public can be accessed from anywhere. This is the most permissive access level.

private: A class, method, or variable that is declared as private can only be accessed within the same class. This is the most restrictive access level.

protected: A class, method, or variable that is declared as protected can be accessed within the same package or by a subclass in a different package.

default (or package-private): A class, method, or variable that does not have an access specifier specified is considered to have default (or package-private) access. This means that it can be accessed within the same package, but not from outside of the package.

It’s worth noting that, for a class, the access specifier can only be public or default. The access specifier for a method or variable can be any of the four access levels.

Additionally, a private method or variable can be accessed from within the same class, but not from a subclass. A protected method or variable can be accessed from within the same package and from a subclass in another package.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Daily Debug
Daily Debug

Written by Daily Debug

I am software engineer, Technical Content Writer, here to help you learn and share my insights on various tech topics.

No responses yet

Write a response