Access Specifiers in Java
There are 3 access specifiers in Java: public, private, protected. There is a <no-access-specifier> i.e a method or class defined without any access specifier.An access specifier tells us which entity cannot be accessed from where in a program.Access modifiers can be defined at two levels, Class level and member level.
Class level
: There are two access specifiers that are valid at the class level. They arePublic and <none>. So, when we define a class, we can do it either of the ways:Public class a{}OR Class a{}If a class is defined as public, it can be accessed from outside world i.e from different packages.If a class is defined w/o any access specifier, it cannot be accessed from different packages.
Member Level
: Member level means for class variables or class methods level.Class a{<access-specifier> int var;}class a{<access-specifier> void method(){}}
Leave a Comment