top of page

JAVA Access Modifiers

What are Access Modifiers in Java?

Access Modifiers are used to scope the variables, methods, and other members of a class. It is used to restrict access to different elements of a class.

Please see below infographic image to better understand the access level of different modifiers in Java.

A class can use just two of the four access control levels (default or public), members can use all four: ■ public

■ protected

■ default

■ private

A Short Explanation:

  • Private: A private member is accessible within the class scope.

  • Default: A member with no access modifier is also known as package-private. Which is only accessible within classes in the same package.

  • Protected: A protected member is accessible within all classes in the same package and the subclasses of the other packages.

  • Public: A public member is accessible to anywhere.

Conclusion The main difference between each visibility level is who can access them: – public – everyone – private – only the class that contains the variable/method – protected – the class that contains the variable/method and any classes derived from it The visibility of the class, method, or variable determines if and how other methods or classes can interact with it. This is called encapsulation, and it’s one of the cornerstones of object-oriented programming. And now that we know what access modifiers are and how they work, we can use them in your own code to make it more readable and maintainable.

84 views0 comments

Recent Posts

See All

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page