Java: Acess Modifiers

a thread...

(Cheatsheet Included)

What is an access modifier?

Access Modifiers are a way of providing limited visibility to methods, variables, classes.

Different access modifiers provide different visibility due to which our program because less prone to unwanted changes.
Java has four access modifiers, let's see each one by one

public:
It's the least restrictive access modifier when any class, method, or variable is defined as public then it is accessible from anywhere.
protected:
Protected methods & variables can be accessed within the same package and also accessible to all child classes.

default:
When no access modifier is specified then java treats it as default. With default methods & variables are accessible within the same package.
private:
It's the most restrictive access modifier, when given to any methods & variable it is only accessible within the same class.
Access Modifier Cheat Sheet:

More from Vikas Rajput

You May Also Like

I'm going to do two history threads on Ethiopia, one on its ancient history, one on its modern story (1800 to today). 🇪🇹

I'll begin with the ancient history ... and it goes way back. Because modern humans - and before that, the ancestors of humans - almost certainly originated in Ethiopia. 🇪🇹 (sub-thread):


The first likely historical reference to Ethiopia is ancient Egyptian records of trade expeditions to the "Land of Punt" in search of gold, ebony, ivory, incense, and wild animals, starting in c 2500 BC 🇪🇹


Ethiopians themselves believe that the Queen of Sheba, who visited Israel's King Solomon in the Bible (c 950 BC), came from Ethiopia (not Yemen, as others believe). Here she is meeting Solomon in a stain-glassed window in Addis Ababa's Holy Trinity Church. 🇪🇹


References to the Queen of Sheba are everywhere in Ethiopia. The national airline's frequent flier miles are even called "ShebaMiles". 🇪🇹