May 3, 2022

Java Access Modifiers - public, private, protected, default

Access level modifiers in Java are used to set the visibility of the class, interface and the visibility of fields, methods and constructors of the class. Access level modifiers determine whether other classes can use a particular field or invoke a particular method of a given class.

Types of access modifiers in Java

There are four Java access modifiers-

  1. Private- The private access modifier specifies that the member can only be accessed in its own class.
  2. Default (package-private)- If no modifier is specified that means default access. With default access modifier class, fields, methods are visible with in the same package.
  3. Protected- Protected access modifier in Java is a little more relaxed than the default access. Class members apart from being visible in the same package can also be accessed by a sub class in any package.
  4. Public- In case of public access modifier, class is visible to all classes everywhere.

Access modifier applicability in Java

A top level class in Java can only be public, or package-private though an inner class can have private or protected access too.

Class fields, methods and constructors can have any of the four access modifiers.

Following table summarizes the access modifiers that can be used with class inner class, field, method and constructor.

private default protected public
class N Y N Y
Inner class Y Y Y Y
field Y Y Y Y
method Y Y Y Y
constructor Y Y Y Y

Private access modifier

Private access modifier in Java can’t be used with a top level class. Having a class that can’t be accessed by any other class is of no use so private access modifier is not permitted with the top level class though a nested class can be marked as private.

Fields or methods that are having the private access modifier can be accessed with in the class where they are declared. Generally class fields are marked as private and if they have to accessed from outside the class that is done through getters and setters which are marked as public.

public class MyClass {
  private String name;
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

public class MainClass {
  public static void main(String[] args) {
    MyClass myObj = new MyClass();
    myObj.setName("knpCode");
    System.out.println("Name- " + myObj.getName());
  }
}
Output
Name- knpCode

A constructor can be marked as private too. If a class has only private constructor then the instance of that class can be created only by that class.

For Example-
public class MyClass {
  private MyClass() {
    
  }
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
Then the following code will result in error “The constructor MyClass() is not visible”.
public class MainClass {
  public static void main(String[] args) {
    // Compile time error
    MyClass myObj = new MyClass();
    myObj.setName("knpCode");
    System.out.println("Name- " + myObj.getName());
  }
}

Default access modifier

If you don’t specify any access modifier with class or its members that means default access which is also known as package-private access.

A class having the default access can only be accessed by a class with in the same package.

Same way fields, methods and the constructors of the class having the default access can only be accessed by a class with in the same package.

For example let’s say we have a class MyClass with methods having default access and this class is in the package com.knpcode.programs

package com.knpcode.programs;

public class MyClass {	
  private String name;

  String getName() {
    return name;
  }

  void setName(String name) {
    this.name = name;
  }
}

Then a class in another package won’t be able to access the methods of the above class.

package com.knpcode;

import com.knpcode.programs.MyClass;

public class MainClass {	
  public static void main(String[] args) {
    MyClass myObj = new MyClass();
    // error
    myObj.setName("knpCode");
    System.out.println("Name- " + myObj.getName());
  }
}

This code will give error “The method getName() from the type MyClass is not visible” and “The method setName() from the type MyClass is not visible”.

Protected access modifier

Protected access modifier gives visibility with in the same package just like default access plus visibility to the sub classes with in the same package or other.

Protected access modifier can’t be used with top level class.

Fields, methods or constructors of the class can be marked as protected.

For example let’s say we have a class MyClass with constructor and methods having protected access and this class is in the package com.knpcode.programs

public class MyClass {
  protected MyClass() {
    System.out.println("in Constructor of MyClass");
  }
  private String name;

  protected String getName() {
    return name;
  }

  protected void setName(String name) {
    this.name = name;
  }
}

Then a class in another package will be able to access the methods of the above class only if it is a subclass of that class.

public class MainClass extends MyClass {
  public static void main(String[] args) {
    MainClass myObj = new MainClass();
    myObj.setName("knpCode");
    System.out.println("Name- " + myObj.getName());
  }
}
Output
in Constructor of MyClass
Name- knpCode

public access modifier

Public access modifier has no visibility restrictions. A class or class member marked as public can be accessed from any other class in any package.

Class member access summary

Following table shows the access levels for the class members with different access modifiers in Java.

Private No Modifier Protected Public
Same class Yes Yes Yes Yes
Subclass in same package No Yes Yes Yes
Another class in same package No Yes Yes Yes
Subclass in different package No No Yes Yes
Another class in different package No No No Yes

That's all for the topic Java Access Modifiers - public, private, protected, default. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment