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.
PublicThe 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.
PrivateThe 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.
ProtectedProtected means that the member will be accessible within that paticular class, within the same pack…