Access Modifiers in Java

Access modifiers define the access control of data, class, and method with whom they defined. Java provides the 4 level of access modifiers.

  • public

  • private

  • protected

  • default

Access modifiers play an important role in Java programming. It’s very important to understand the concept of access modifiers in Java.

 

Private Access Modifiers

  • Methods or variables declared as private can be accessed with the same class only.
  • Subclass can’t access the private element of its super class.
  • Class within the same package can’t access the private elements of each other.
  • Class can’t be marked as private. (If class marked as private then you can’t inherit that class in future it violates the OOPS concept)

 

Public Access Modifiers

  • Public modifier means whole Java world can access these elements regardless where the accessing code is located.
  • When you accessing the public class from some other packages you just need to import that package in your code.

 

Default Access Modifiers

  • Default access modifiers mean, no access modifier declared for the class, variable or constructor.
  • Default elements can be accessed within the same class and the classes presented in the same package.
  • Default elements can’t access by the subclass unless the subclass located in the same class.

 

Protected Access Modifiers

  • Protected members can be accessed within the subclass as well. Rest it behaves like default modifiers.
  • A subclass can access the protected members of the superclass as well.
  • Methods declared protected in the superclass must be protected or public in subclass they can’t be private.

 

Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

1 Comment
  1. Priyanshi Ahuja 10:32 AM / August 22, 2016 - Reply

    Nice Information, Please upload the Tutorial on Multi-threading as well … Thank You

Leave a Reply