What access modifier for a class means that access to the class is not limited?

Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are −

  • Visible to the package, the default. No modifiers are needed.
  • Visible to the class only (private).
  • Visible to the world (public).
  • Visible to the package and all subclasses (protected).

Default Access Modifier - No Keyword

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.

A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.

Example

Variables and methods can be declared without any modifiers, as in the following examples −

String version = "1.5.1";

boolean processOrder() {
   return true;
}

Private Access Modifier - Private

Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.

Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.

Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.

Example

The following class uses private access control −

public class Logger {
   private String format;

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

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

Here, the format variable of the Logger class is private, so there's no way for other classes to retrieve or set its value directly.

So, to make this variable available to the outside world, we defined two public methods: getFormat(), which returns the value of format, and setFormat(String), which sets its value.

Public Access Modifier - Public

A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.

Example

The following function uses public access control −

public static void main(String[] arguments) {
   // ...
}

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

Protected Access Modifier - Protected

Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.

Example

The following parent class uses protected access control, to allow its child class override openSpeaker() method −

class AudioPlayer {
   protected boolean openSpeaker(Speaker sp) {
      // implementation details
   }
}

class StreamingAudioPlayer extends AudioPlayer {
   boolean openSpeaker(Speaker sp) {
      // implementation details
   }
}

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that’s why we have used protected modifier.

Access Control and Inheritance

The following rules for inherited methods are enforced −

  • Methods declared public in a superclass also must be public in all subclasses.

  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.

    Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class. The four access modifiers in Java are public, protected, default, and private.

    Four Types of Access Modifiers

    • Private: We can access the private modifier only within the same class and not from outside the class.
    • Default: We can access the default modifier only within the same package and not from outside the package. And also, if we do not specify any access modifier it will automatically consider it as default.
    • Protected: We can access the protected modifier within the same package and also from outside the package with the help of the child class. If we do not make the child class, we cannot access it from outside the package. So inheritance is a must for accessing it from outside the package.
    • Public: We can access the public modifier from anywhere. We can access public modifiers from within the class as well as from outside the class and also within the package and outside the package.

    For a refresher, check out this free course on Java programming. On completion, you will also earn a certificate which is sure to put you ahead in the competitive space.

    Must Learn Java Concepts

    Java Java Algorithms Java Data Structures JDBC In Java OOPs In Java Java Basic Programs

    Let us see which all members of Java can be assigned with the access modifiers:

    Members of JAVAPrivateDefaultProtectedPublicClassNoYesNoYesVariableYesYesYesYesMethodYesYesYesYesConstructorYesYesYesYesinterfaceNoYesNoYesInitializer BlockNOT ALLOWED

    Now let us understand the scope of these access modifiers with the help of a table:

    AccessibilityPrivateDefaultProtectedPublicSame PackageSame ClassYesYesYesYesWithout InheritanceNoYesYesYesWith InheritanceNoYesYesYesDifferent PackageWithout InheritanceNoNoNoYesWith InheritanceNoNoYesYes

    Let’s understand with more details:

    Also, check Top Java Interview Questions and Answers for Freshers

    Private Access Modifier

    • The private access modifier is specified when any member of a class is prefixed with the private keyword. In comparison with the other access modifiers, this is the most restricted access modifier. 
    • When the methods or data members are prefixed with a private access modifier, the visibility of these methods and data members are restricted so, they can be accessed only within the same class where they have been declared, they will not be visible to the outside world. 
    • If we have another class from the same package still, we will not be able to access these methods or data members. So usually, we keep the class variables and methods as private, which are intended to be used inside the same class where declared.  

    Let us consider an example where we will consider two classes A1 and A2 within the same package p1. We will declare a variable and a method as private in class A1 and then try to access these methods and variables from class A2. 

    So here we will Compile Time Error.

    Let us see for a private constructor:

    If we make any class constructor private, we cannot create the instance of that class from outside the class, and hence, from here we can conclude that the private access modifier can be accessed only within the same class and not from outside the class.

    Default Access Modifier

    • It is not a keyword. Any Java members such as class or methods or data members when not specified with any access modifier they are by default considered as default access modifiers.  These methods or data members are only accessible within the same package and they cannot be accessed from outside the package. It provides more visibility than a private access modifier. But this access modifier is more restricted than protected and public access modifiers.

    Let us consider an example for the default access modifier.

    Here, we have two different packages p1 and p2. In the p1 package, we have class A1 where we declared a default variable and a default method. Now we are trying to access this variable and method from outside the package that is from package p2 which has a class A2. 

    When we try to access these variables and methods from outside the package we get a Compile time error.

    Hence, we conclude that the default access modifier members can be accessed only within the same package and cannot be accessed from outside the package. And they have more visibility than private access modifier but is more restricted than protected and public access modifiers.

    Protected Access Modifier

    • It is a keyword. This access modifier is used to access the methods or data members of a class within the same package as well as outside the package but only through inheritance. The protected access modifier has more accessibility than private and defaults access modifiers. But it has less visibility than the public access modifier.

    Let us consider an example for a protected access modifier. 

    Here we have two packages p1 and p2. In package p1 we have class A1 where we have declared a protected test method. In package p2 we are inheriting the members of class A1 inside class A2 with help of extending keywords and creating a relationship between the two classes. We can also say that class A1 is the parent class or the superclass and class A2 is the child class or the subclass respectively.

    When we inherit the members of class A1 inside class A2, with the help of a protected access modifier we can access the members of class A1 of package p1 from class A2 of the different package p2.

    So here we get the output as Hi I’m from a protected method. 

    Hence, we can conclude that the methods, variables, and data members of a class prefixed with a protected access modifier can be accessed within the same package as well as can be accessed from outside the package but only with the help of inheritance.

    Public Access Modifier

    It is a keyword. If a class member like variable, method, or data members are prefixed with a public access modifier, then they can be accessed from anywhere inside the program. That is, they can be accessed within the same class as well as from outside the different classes. 

    It also includes access within the same package and also from outside the package. The members like variables, methods, and other data members can be accessed globally. 

    Using public access modifiers we can provide access to the members most simply. There are no restrictions on public access modifier members. Hence, it has the widest accessibility or visibility scope as compared to the rest of the access modifiers.

    Let us now consider an example of public access modifier.

    Here in this example, we have two different packages p1 and p2. In p1 we have a class a1 where we have declared a variable and a method prefixed public keyword. And in the p2 package, we have a class A2 from where we are trying to access the members of class A1 without inheritance.

    Here we get the output as 10 and Hi I’m from the public method.

    So from the above example, we can conclude that public access modifier members can be accessed from anywhere, within the same class as well as from outside the class. And also can be accessed within the same package and also from outside a package.

    NOTE: If any other developer is using your class, then try to use the most restricted access modifier. And also try to use a private access modifier, wherever necessary.

    An overall accessibility:

    private<default<protected<public.

    JAVA Access Modifiers with Method Overriding

    When overriding a method, the method which is overridden should not be restrictive.

    For example:

    In the above example, the test method is been overridden in class A2. But the subclass method should have the same visibility or more visibility than the superclass method. Since the subclass method has less scope than the superclass method, we get a compile-time error.

    What access modifiers that the class is accessible by any other class?

    Access Modifiers.

    What is the access modifier for a class?

    What are Access Modifiers? Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class. The four access modifiers in Java are public, protected, default, and private.

    What access modifier that can be accessed from within the class outside the class within the package and outside the package?

    Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

    Which of the following access modifier makes a class limited to the current assembly?

    The following seven accessibility levels can be specified using the access modifiers: public : Access isn't restricted. protected : Access is limited to the containing class or types derived from the containing class. internal : Access is limited to the current assembly.