Access Modifiers in Java

As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member. There are four types of access modifiers available in java:

 *Default– No keyword required

*privater

 *protected

*Public.

Default When no access modifier is specified for a class , method or data member – It is said to be having the default access modifier by default.

Thedata members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.

Private: The private access modifier is specified using the keyword private.

  • The methods or data members declared as private are accessible only within the class in which they are declared.
  • Any other class of same package will not be able to access these members.
  • Top level Classes or interface can not be declared as private becaus
  • private means “only visible within the enclosing class”
  • protected means “only visible within the enclosing class and any subclasses”
  • protected: The protected access modifier is specified using the keyword protected.
  • The methods or data members declared as protected are accessible within same package or sub classes in different package.
Posted on by