HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Object Oriented Programming / Access Level Modifiers

Java – Access Modifiers

Java provides four access modifiers to set access levels for classes, variables, methods and constructors i.e. public, private, protected and default. These access level modifiers determine whether other classes can use a particular field or invoke a particular method.

1. Java access modifiers

Let’s quickly compare these access modifiers in nutshell.

  1. public – accessible everywhere
  2. protected – accessible in the same package and in sub-classes
  3. default – accessible only in the same package
  4. private – accessible only in the same class

The access specifiers can be strictly ordered as below :

public > protected > package-private (or default) > private

1.1. public

Public are accessible from everywhere. A class, method, constructor, interface, etc declared public can be accessed from any other class.

However, if the public class we are trying to access is in a different package, then the public class still needs to be imported.

public class HelloWorld 
{
   private String format;

   public String getFormat() {
      return this.format;
   }
   public void setFormat(String format) {
      this.format = format;
   }
}

In above example, getFormat() and setFormat() methods are public, so they can be accessed anywhere.

1.2. protected

Protected are accessible by the classes of the same package and the subclasses residing in any package. Protected access gives the subclass a chance to use the helper method or variable while preventing a non-related class from trying to use it.

public class HelloWorld 
{
   private String format;

   protected String getFormat() {
      return this.format;
   }
   protected void setFormat(String format) {
      this.format = format;
   }
}

In above given example of HelloWorld, variable format is declared protected, so it can be accessed by all the classes present in same package where HelloWorld.java is present, as well as sub-classes present in other packages as well.

1.3. default (package private)

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. Default are accessible by the classes of the same package.

Please note that the fields in an interface are implicitly public static final and the methods in an interface are by default public.

public class HelloWorld 
{
   String format;

   public String getFormat() {
      return this.format;
   }
   public void setFormat(String format) {
      this.format = format;
   }
}

In above given example of HelloWorld, variable format is declared default, so it can be accessed by all the classes present in same package where HelloWorld.java is present.

1.4. private

A private access modifier is the most restrictive access level. (Topmost) Classes and interfaces cannot be private. private members are accessible within the same class only. Methods, Variables, and Constructors that are declared private can only be accessed within the declared class itself.

In above given example of HelloWorld, variable format is declared private, so no class can access it directly. It must be accessed though public methods getFormat() and setFormat().

Access levels affect you in two ways. First, when you use classes that come from another source, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Local variables and formal parameters cannot take access specifiers. Since they are inherently inaccessible to the outside according to scoping rules, they are effectively private.

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

2. Levels of Access Control

There are two levels of access control.

  1. Class level — Allowed modifiers are public, or package-private (default).
  2. Method level — Allowed modifiers are public, private, protected, or package-private (default).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package.

For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class.

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Both private and protected can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.

Happy Learning !!

Ref: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Justin

    December 6, 2019

    Variable format is not declared protected in 1.2, it is private.

  2. Chetan Kottapalli

    December 27, 2016

    Can we apply more than one execution level modifier in java?

Comments are closed on this article!

Search Tutorials

Java OOP

  • OOP – Introduction
  • OOP – Access Modifiers
  • OOP – Constructors
  • OOP – Instance Initializers
  • OOP – Abstraction
  • OOP – Encapsulation
  • OOP – Inheritance
  • OOP – Polymorphism
  • OOP – Overloading vs Overriding
  • OOP – Interface vs Abstract Class
  • OOP – extends vs implements
  • OOP – instanceof operator
  • OOP – Multiple Inheritance
  • Association, Aggregation and Composition

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)