Access Modifiers in Java

When we define attributes or methods in a class, we can access those attributes and methods by creating that class object. If the method is static, it can be accessed using class name. For example if there is a class named Student, we can create its object and access its methods, lets see the code sample.


public class Student {
    int id;
    String name;
    void registerCourse(String courseCode){
          System.out.print("registerCourse called with code: " + courseCode);
    }
}

public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.id = 1;
        student.name = "Alice";
        System.out.println("ID : " + student.id);
        System.out.println("Name : " + student.name);
        student.registerCourse("CSC245");
    }
}

In StudentTest class, we have created the Student object and accessed id and name attribute using the object name. We named the object 'student' and accessed the id and name using objectName.attributeName notation i.e. student.id or student.name

In OOP, sometime a class may need to define methods or attributes that should be accessible only from same class and not in the other classes where its object is created. In other words, a class may need to define some state or behavior which is its private and not available to other classes. On the other side, a class may decide to make a particular attribute or method accessible to all classes where its object is created. Access modifiers are used to define such rules or policies.

As the name suggest, access-modifiers modify the access of an attribute or method i.e. they decide from where the attribute or method can be accessed.

There are different access modifiers used with attributes and methods. These are just some keywords which are used when we declare an attribute or method. The access-modifiers are public, default, private and protected. public, private and protected are keywords.

default Access Modifier in Java

When you specify no access-modifier when declaring an attribute or method, default access modifier is used implicitly. To understand default access-modifier, you must know what are packages in Java, why and how they are used. (I would write a detailed post on package later), for the time being, assume packages are just folders where related code is placed. and a software contains many packages.

In above example, we have used no access-modifier with id, name, and registerCourse. Its mean  the access modifier of id, name and registerCourse is default. The attributes and methods with default access-modifier can be accessed by creating Student object, but only if the class that creates the Student object reside in same package where Student class reside. Otherwise, we can't access id, name and registerCourse. In above example, assume Student and StudentTest are in same folder, so accessing the attributes and methods of Student object in StudentTest class is valid.

But if we place the StudentTest class in some other package, we can not access id and name attribute and registerCourse method.

public Access Modifier in Java

If you declare an attribute or method by public access modifier, its mean the attribute or method can be accessed from anywhere. Even if the client classes (e.g. the StudentTest class or any class that make the object of Student class) reside in different package. public can be accessed from anywhere.

See below modified code:

public class Student {
    public int id;
    public String name;
    void registerCourse(String courseCode){
        System.out.print("registerCourse method called with cource code: " + courseCode);
    }
}

public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.id = 1;
        student.name = "Alice";
        System.out.println("ID : " + student.id);
        System.out.println("Name : " + student.name);
        student.registerCourse("CSC245");
    }
}

You see, id and name are public in Student class. So they can be accessed from anywhere where Student object is created. Even if the StudentTest class reside in different folder as compare to folder where Student class is placed.

private Access Modifier in Java

If private access modifier is used with an attribute or method, it indicates the attribute or method can be accessed only from inside the same class. Other classes, whether they reside in same package or in different package, they can not access private attributes and methods.

For example, if you change the Student class definition to:

public class Student {
    private  int id;
    private String name;

    private void registerCourse(String courseCode){
        System.out.print("registerCourse method called with cource code: " + courseCode);
    }
}

And Run the StudentTest class defined above, it would generate following errors:

java: id has private access in Student
java: name has private access in Student
java: registerCourse(java.lang.String) has private access in Student

So from the error message, you can clearly conclude the private methods and attributes are personal assets of the class and it do not allow others to access them.

Another Example of private Access Modifier

We said, the private attributes and methods are accessible only from same class, in this example, I would demonstrate how they are accessed inside the same class. Assume a class Student, with following definition:

public class Student {
    private  int id;
    private String name;

    public void displayInfo(){
        System.out.println("Id: " + id );
        System.out.println("Name: " + name );
    }

    public void method2(){
        registerCourse("CSC245");
    }

    private void registerCourse(String courseCode){
        System.out.print("registerCourse method called with cource code: " + courseCode);
    }
}

1. See id and name is private, but its accessible inside the displayInfo method, because this method reside in same class where private attribute reside.

2. registerCourse is private and can't be called from other classes by making Student class object. But you can call the registerCourse method from another method e.g. method2, because method2 reside in same class where registerCourse method reside. Please note, the access modifier of method2() do not matter, as far as calling registerCourse is concern.

protected Access Modifier

To understand protected access modifier, you must know what is inheritance in Java because the protected access modifier is used when we inherit one class from another, so I would cover protected access-modifier in "Inheritance in Java" section. As far basics of access modifiers are concern, understanding default, public and private well is good enough.

Comments

  1. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing...
    Regards,
    JAVA Training Chennai|JAVA J2EE Training in Chennai

    ReplyDelete
  2. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Regards,
    PHP Training in Chennai||PHP Course in Chennai

    ReplyDelete
  3. Nice blog, here I had an opportunity to learn something new in my field. I have an expectation about your future post so please keep updates.
    Regards,
    web designing course in chennai|web designing training

    ReplyDelete

Post a Comment