Java Access Modifiers-Public, Private, Protected, Default

Sandhya Reyya
2 min readSep 21, 2021

We have different access modifiers in java either to inherit, reuse or to retrieve either instances or behavior of a class. Access modifiers are of 4 types.

  1. Public
  2. Private
  3. Protected
  4. Default

Public :

The data or the methods are available within the class, outside the class, within the package, and also outside of the package.

Let us understand more clearly

“The Father of Computing” is “Charles Babbage”. It is the same for all the countries in the world. The truth(consider truth as our data or method is accepted(inherited or retrieved) by his own country and all the remaining country(within the class or package and outside the class or package).

Private:

The data or the methods are just restricted to their own class.

The example taken to this access modifier is an MNC company.

Best employer of the year”(data or methods ) in the main branch(class) of the company(package) is just existing and restricted to that company itself but not even to other branches(sub-classes) of the same company.

Protected:

The data or the methods are restricted to their own class and subclasses.

“ CEO” of GOOGLE is “SUNDAR PICHAI”(data or methods), who is the CEO of the home branch(own class) and sub-branches(sub-classes).

Default:

The access level of the data or the methods is at its own package itself. If we don’t specify any access modifier, by default “Default” is assigned.

“Father of the Nation” in India is “Mahatma Gandhi”(data or methods) restricted to his own nation(own package) but not to the other countries(other packages).

I hope, the content helps to know about the access modifiers in java.

Comment some topic names which I need to write the future stories.

Thanking you,

Sandhya Reyya.

--

--