Difference between Public, Private and Protected modifier
in Java?
In
Java, you have got something called access modifier, which specifies
accessibility of class, methods and variables. There is four access modifier in
Java namely public, private, protected and the default access modifier, also
known as package level modifier.
The difference between these access modifier comes in their ability to restrict
access to a class, method or variables, public is the least
restrictive access modifier while private is the most restrictive access
modifier, package and protected lies in between. Another key difference
between public, protected, package and private modifier come from the
point where you can apply them, for example, you cannot use private or protected modifier with
a top level class but you can use public modifier there.