Let’s refresh some common but important topic of programming languages – Access Modifiers!
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control.
While using Java, we may need to use them at top level and member level.
I will start with DEFAULT: This means we do not explicitly declare an access modifier for a class, field, method etc. It is available to any other class in the same package but cannot be used for methods, fields in an interface.
PRIVATE:Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. Class and interfaces cannot be private. Used for encapsulation and hiding data.
PUBLIC: A class, method, constructor, interface etc declared public can be accessed from any other class. However if the public class we are trying to access is in a different package, then the public class still need to be imported.
PROTECTED : Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class.The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.