Showing posts with label Access specifiers in java. Show all posts
Showing posts with label Access specifiers in java. Show all posts

Sunday, 9 December 2018

Access Specifier in java | Java Access Specifiers - Online Help

Access Specifier in Java:

Access Specifier in java otherwise called visibility control or access control or access modifiers. Access control is used to restrict access to certain variables and methods from outside the class.


Access Specifiers in Java has the following four types of visibility modifier. They are,
                    1.Private
                    2.Public
                    3.Protected
                    4.Package

Private:

                It is accessible only by methods of this class (accessible only by the class in which it is declared).
Example:
private int temp;
private int emp_salary;
private double netsalary()

                The private keyword makes sure that the only methods that can access these instance files are the methods of the employer class itself. In the private keyword, no outside method can read or write to these fields.

Public:

               Access Specifier in java has one access specifier which is called as public. It is accessible my methods of all classes(Accessible outside the class in which it is declared).
Example:
Public double netsalary()
public String read()
public int sum()

                The public keyword makes sure that any method in any class can call the method.

Protected:

                It is accessible only from within its own class and its subclasses.
Example:
protected int number;
protected int sum()

 Package:

               A package is the default level of access in Java. If we do not specify an access control, then the default level is package access(no access specifier is specified for variables and methods are accessible by all the classes defined in the package in which it is defined)