Monday, 4 May 2020

Access Specifiers and Modifiers in Java


Classes enable an object to access data variable or method of another class. For Example. In a banking application you might need to hide information such as customer balance, form unauthorized access by other classes of the application.
Why do we use access specifiers? - Quora


Access Specifiers:-An access specifier control the access of class members and variables by other class. The various type of access specifiers in java.
·         public
·         Private
·         Protected
·         Friendly or package

T     The Public Access Specifier:-Class member with public access Specifier can be accessed anywhere in the same class..     

P    Public keyword is used  to declare a member as public. The following code snippet shows how to declare a data member of a class as public:

P     public  <data type>  <variable name>


T     The Private Access Specifier:- The private access specifier provides most restricted level access. The access level of a private modifier is only with in the class. It cannot be accessed from outside the class.



T      The Protected Access Specifier:-The variable and methods that are declared protected are accessible only to the subclasses of the class in which they are declared.





T      The friendly or package access specifier:- If you do not specify any access specifier, the scope of data members and methods is friendly. Java provides a large number of class, which are organized into groups in a package. A class, variable or method that has friendly access is accessible only to the class of a package. 

0 comments:

Post a Comment