Posts

Showing posts from August 14, 2020

Access modifiers in Java

Image
  "Which access modifiers are you familiar with and what is the difference between them?" This is a very common interview question, that can at times catch a candidate off guard. In this post, we will discuss the different access modifiers available in Java, and the differences between them. In Java, there are 4 main access modifiers that are basically reserved keyword which allow us to control access to fields, methods and classes from other entities. Confused? Let's see if we can clear things up a bit. Public The public keyword means that this class/member/method can be accessed from anywhere. And by anywhere I mean that any class, package, method can access the field defined as public. Private The private modifier limits the access of a particular member/method to the specific class where it was initialised or declared. The private keyword cannot be applied to classes. Protected Protected means that the member will be accessible within that paticular class, within the