My JAVA story: Visibility Modifiers

Chinonso Okoroafor
3 min readFeb 19, 2019

--

Source: Google

This is Day one of my Java journey since I promised not to bore you out with some basics such as loops and variable declaration(you can pick them up from just any Java programming book, hopefully without issues), I will want to ask to be permitted to do something on visibility modifiers. For the Pythonic folks like me, this topic might look weird, since python doesn’t have an explicit way of declaring the visibility of objects.

So, in simple terms, visibility modifiers are ways Java ‘exposes’ variable and objects to other parts of the code. The language provides three access modifiers, they are; Public, Private and protected. There is a default modifier too(making it four) So we will go over this one after the other, and explain with codes how they work.

Public: As the name implies, this modifiers allows every methods and data field in a class to be accessible from any package or class as long as an instance(object) of the class has been created. That may sound like gibberish, but let us grind some of the big jargon down.

Package: a package can be seen as similar to the different folders on your computer.

Class: This is a blueprint of objects to be created, it defines the data and methods that will be needed by an object both for its initialization and manipulation. Let us be a bit more realistic using a real life example, it is the design of a house created on a piece of paper, from this single design, different units of houses can be built.

Objects: This is also known as an instance. Like every real world object, they have state and behaviors which are defined while creating the template(class). Note that this states and behaviors can be modified (depending on the access specifier)

Private: Every data field or method declared with this access specifier can only be accessed by objects of the class it was created. Don’t worry, I will show an example of this specifiers, and hopefully, you appreciate them more.

Protected: If a mum wants to mark a bag of orange, so her children can access it no matter where they find the bag of orange(be it Lagos or Owerri) they have access to it. She marks it as “protected”. In Java lingo, a child of a Class is called a “subclass”. No matter where the subclasses were created, be it in the same package or otherwise, they will be able to access any data field or method marked protected, while other objects can not.

Default: Anytime a programmer fails to define a visibility modifier, Java automatically sets it to default. And the implication of this is that only objects of the class which contains the default variable, defined in the same package can access all the properties and methods ‘marked’ default in the class.

source: Introduction to Java programming(Y Daniel Liang)

I hope this diagram above helps to clarify all I have said up there, in my next post tomorrow, we will be taking another step further in the Java journey. Till then, keep doing shit.

--

--

Chinonso Okoroafor
Chinonso Okoroafor

Written by Chinonso Okoroafor

SoftwareEngineer|Python|Java|Golang|Poet and a thousand and one things.