Skip to main content

Posts

Access can be package - Private, Protected and Public ( Differences )

Java has four access modifier namely  private ,  protected  and  public . package level access is  default access  level provided by Java  if no access modifier is specified. These access modifiers are used to restrict accessibility of a class, method or variable on which it applies. We will start from private access modifier which is most restrictive access modifier and then go towards public which is least restrictive access modifier, along the way we will see some best practices while using access modifier in Java and some examples of using  private  and  protected  keywords. private keyword in Java private  keyword or modifier in java can be applied to member field, method or nested class in Java. you can not use the  private  modifier on top level class.  private  variables, methods, and class are only accessible on the class on which they are declared.  private  is the highest form of Encapsulation Java API provides and should be used as much as possible. It's best c