Access Modifiers:
- The access modifiers in Java specifies the accessibility or scope of a variable, method, constructor, or class.
- We can change the access level of variables, constructors, methods, and class by applying the access modifier on it.
- There are four types of Java access modifiers:
- private
- public
- default
- protected
Private:
- The access level of a private modifier is only within the class.
- It cannot be accessed from outside the class.
Public:
- The access level of a public modifier is everywhere.
- It can be accessed from within the class, outside the class, within the package and outside the package.
————————————————————————–
SCENARIO 1:
- In this example, we have created two classes StateBank and SBICustomer.
- StateBank class contains private variables.
- We are accessing these private variables from outside the SBICustomer class, so there is a compile-time error.
StateBank Class:
public class StateBank
{
private int minBalance = 1000;
private String custName;
public void deposit()
{
System.out.println("Deposit");
}
public void withdraw()
{
System.out.println("Withdraw");
}
}
SBICustomer Class:
- This SBICustomer class is common for all scenario.
public class SBICustomer
{
public static void main(String[] args)
{
StateBank rep = new StateBank();
rep.deposit();
rep.withdraw();
System.out.println(rep.minBalance);
}
}
ERROR:
- minBalance has private access in StateBank
- System.out.println(rep.minBalance);
Solution:
- Change the variable access type into public or default.
————————————————————————–
SCENARIO 2:
- StateBank class contains private methods.
- We are accessing these private methods from outside the SBICustomer class, so there is a compile-time error.
StateBank Class:
public class StateBank
{
int minBalance = 1000;
String custName;
private void deposit()
{
System.out.println("Deposit");
}
private void withdraw()
{
System.out.println("Withdraw");
}
}
Refer Scenario 1 for SBICustomer class.
ERROR:
- deposit() has private access in StateBank
- rep.deposit();
- withdraw() has private access in StateBank
- rep.withdraw();
Solution:
- Change the method access type into public.
————————————————————————–
SCENARIO 3:
- StateBank class is a private class.
- We are accessing these private class from outside the SBICustomer class, so there is a compilation error.
StateBank class:
private class StateBank
{
int minBalance = 1000;
String custName;
public void deposit()
{
System.out.println("Deposit");
}
public void withdraw()
{
System.out.println("Withdraw");
}
}
Refer Scenario 1 for SBICustomer class.
ERROR:
- modifier private not allowed here
- private class StateBank
Solution:
- Change the class access type into public.
————————————————————————–
SCENARIO 4:
- Constructor access type is private.
- So we get compilation error.
StateBank Class:
public class StateBank
{
int minBalance = 1000;
String custName;
private StateBank()
{
System.out.println("Zero Argument Constructor");
}
public void deposit()
{
System.out.println("Deposit");
}
public void withdraw()
{
System.out.println("Withdraw");
}
}
Refer Scenario 1 for SBICustomer class.
ERROR:
- StateBank() has private access in StateBank
- StateBank rep = new StateBank();
Solution:
- Change the constructor access type into public.
————————————————————————–