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()
No comments:
Post a Comment