Access Modifiers (public, private, protected)

Access Modifiers (public, private, protected)

Access modifiers are class features used in Java programming to define program visibility of user-declared program function methods and variable parameters, or to define user class data accessibility within the current program. Access modifiers allow Java programmers to manage which classes or class components, such as class function methods, data types, or fields, can access and control a particular class member within the current program, whether public, private, or protected.

Access Modifiers (public, private, protected)

There are three main types of access modifiers in Java programming.

  • Public access modifier.
  • Private access modifier.
  • Protected access modifier.
  • Default/no access modifier specified.

All the class access modifiers declared above provide different features and control the access level of these access modifiers in the class program as needed.

Java Public Access Class Modifier.

The public access modifier in a Java class program allows a user-defined class, class function, method, or user-declared parameter variable to be accessed by any other class, regardless of whether it is used or declared in any class package.

Features of the Java Public Access Class Modifier.

  • Class – A public class data element declared in a Java program can be accessed and controlled from anywhere within the current class.
  • Method – A user-declared public method in a class can be called from any other class at any time.
  • Variable – A user-defined class data variable declared as public can be directly accessed and modified from another class.

Example of the Java Public Access Class Modifier.

public class Employee {

// Here defines a public class instance variable

public String emp_name;

public String age;

public int salary;

// Here defines a public calling method

public void empInfo() {

System.out.println(“Employee name – ” + emp_name);

System.out.println(“Employee age – ” + age);

System.out.println(“Employee salary – ” + salary);

}

}

In the Java Public Access Class example.

  • Here the employee class is a publicly declared class data type, meaning it can be directly accessed and managed from any other class in the same package.
  • Here in the Employee class, the emp_name, age, and salary variables, as well as the empInfo() method, are defined in public order and can be directly accessed, modified, or controlled from other classes.

Accessing Java Public Class Members.

public class Main {

public static void main(String[] args) {

Employee testEmployee = new Employee();

testEmployee.emp_name = “Siddhi Deora”; // here it is accessing the employee class public variable emp_name

testEmployee.age = “19”; // here it is accessing the age employee class public variable

testEmployee.salary = “14000”; // here it is accessing the salary employee class public variable

testEmployee.empInfo(); // here it is calling the empInfo() public class method

}

}

Java Private Class Access Modifier.

Private class access modifier members declared in a Java program provide permission to call and access the existing parameter variables or class methods only to members within the same class in which they are declared with private nature. Remember, private class members cannot be accessed or called from outside the class; they are restricted in nature. Furthermore, you cannot access or call a privately declared class member from a subclass or another class in the same package.

Features of the Java Private Class Access Modifier.

  • Class – Java users cannot declare a private class at the top level with the private class access modifier. The private class access modifier is only used for internal class members.
  • Method/Variable – Remember, a private class method or parameter variable declared in a Java program can only be accessed and controlled internally within the class where it is declared or defined.

Example of a Java Private Class Access Modifier.

public class Employee {

// here we declare private instance class variables data type

private String emp_name;

private String age;

private int salary;

//here we define Private method

private void empInfo() {

System.out.println(“Employee emp_name – ” + emp_name);

System.out.println(“Employee age – ” + age);

System.out.println(“Employee salary – ” + salary);

}

// here Public setter method used to allow controlled access to private class variables data type

public void setMake(String emp_name) {

this.emp_name = emp_name;

}

// here Public getter method used to allow controlled access to private variables data type

public String getMake() {

return emp_name;

}

}

In the Java Private Class Access Modifier example.

  • The private Employee class access modifier defines the private field methods emp_name, age, salary, and empInfo().
  • These members, declared in the Employee class, can only be accessed and modified within the Employee class. Furthermore, the setMake() and getMake() methods are defined in the public order. These provide you with controlled access to private variables in the Employee class.

Accessing Java Private Members.

public class Main {

public static void main(String[] args) {

Employee testEmployee = new Employee();

testEmployee.setMake(“Siddhi”); // Here, access the private employee class variable via the public method

System.out.println(testEmployee.getMake()); // here it is: Accessing employee class private variable via public method

}

}

Java Protected Class Access Modifiers.

Declaring protected class access modifiers in a Java class program allows you to access only the same class members, parameter variables, and class methods. Protected class access modifiers allow the same declared and defined class variable data type to be used within the same package.

With this, you can access protected class access modifiers within subclasses or even subclasses in different packages.

Features of Java Protected Class Access Modifiers.

  • Classes – Protected data types declared in a Java class program cannot be declared or defined within the top-level class. This means they can only be used within inner classes.
  • Methods/Variables – A protected class method or parameter variable declared in a Java program can be accessed and managed by subclasses within the same package, even if they are defined or declared in different classes or packages.

Example of Java Protected Class Access Modifiers.

public class Employee {

// Here we declare the Protected instance variable in the employee class

protected String emp_name;

protected String age;

private int salary;

/ Here we define the Protected employee class method

protected void empInfo() {

System.out.println(“Employee emp_name is – ” + emp_name);

System.out.println(“Employee age is – ” + age);

System.out.println(“Employee salary is – ” + salary);

}

}

Java Protected Class Access Modifiers Example.

  • Here, emp_name, age, salary, and empInfo() are protected class members defined in the Employee class. They can be accessed and managed by subclasses within the same package, even if the subclass is declared or defined in a different package.

Accessing Java Protected Members Within the Same Package.

public class Main {

public static void main(String[] args) {

Employee testEmployee = new Employee();

testEmployee.emp_name = “Harry”; // Here it is accessing the protected employee class variable within the same package

testEmployee.empInfo(); // Here it is calling the protected employee class method within the same package

}

}

Accessing Protected Members/subclass in a different package.

// Here it is calling the protected employee class method within the same package

package anotherPackage;

import somePackage.Employee;

public class Itemployee extends Employee {

public void showDetails() {

emp_name = “Vivek”; // here it is accessing the employee class protected variable in the subclass

empInfo(); // here it is calling the protected employee class method in the subclass

}

}

Java default access/no modifier.

If the Java programmer has not defined any access modifier Behavior (public, private, or protected) in the class program, then the variable data type parameter members declared in the class have default class data access. Generally, the default access modifier is known as class package-private.

Features of the Java default access/no modifier.

  • Class – A class without access modifiers in the class program can only be accessed and managed within the same class package.
  • Method/Variable – Remember, a class method or parameter variable without a class access modifier can only be accessed and managed within the same class package.

Example of the Java default access/no modifier.

public class Employee {

// Here we define a Default Employee class data type with a package-private instance variable

String emp_name;

String age;

int salary;

// Here we define a Default Employee class package-private method

void empInfo() {

System.out.println(“Employee emp_name is -” + emp_name);

System.out.println(“Employee age is -” + age);

System.out.println(“Employee salary is -” + salary);

}

}

Java default access/no modifier example.

  • Here, in the default class data access program, emp_name, age, salary and empInfo() are defined as package-private or default.
  • Default class data can be accessed only within the same class package and cannot be accessed from any other class package.

Leave a Reply