Role of Access Modifiers in Java Inheritance

What methods or instance attributes are inherited is decided by their access modifiers in parent class. Private attributes and methods are not inherited in subclasses. All public attributes and methods are inherited in sub-classes. So you noticed, by making ID and name private in Person class, we stopped their direct availability in subclass Student. And by making get/set methods public for both attributes, we allowed Student class to read/update the inherited attributes without violating the encapsulation principle. I hope you already know, if a class contains some public methods/attributes, they can be called or accesses from reference of that class's object, no matter where the object of that class is created e.g. a static main method or any where else.

There is another access modifier which is specially used with inheritance, its called protected. Protected access modifier is lighter than private but stronger than public access. Protected attributes or methods in parent class, are inherited in subclasses. That means, subclasses can access the protected attributes and methods. But if you create an instance of the parent class from another class that do not reside in same package as the parent class, the protected attributes or methods would not be accessed from the parent class object reference. Its very important, let me demonstrate you the behavior of protected access modifier with an example. (I will gradually improve the example, so that you understand the concept well, so keep reading with patience).


public class Person {
    protected int ID;


public class Student extends Person {
    private String courseName;

    public Student(int ID, String courseName) {
        this.ID = ID;
        this.courseName = courseName;

You see we have initialized the protected attribute value in Student constructor. As discussed earlier, public inherited attributes are also accessible in subclasses, then what is the difference between public and protected access? The difference can be understood if you make an object of Student class, from another class that reside in different package. You would notice, we can't access the ID attribute when its protected. But if ID had public access modifier in Person class, the ID can be accessed from any Student reference. See below, example of invalid access.

public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student(1, "Object Oriented Programming");
        // let, we want to change ID, after the object is created
        student.ID = 10; // INVALID LINE

If you compile it, the compiler would generate following error:
ID has protected access in

Its seems, if we want to make an attribute inheritable but not accessible to user-classes, we should make it protected. But if you notice deeply, by providing read/write access to ID attribute, we have compromised the encapsulation principle. In fact, protected access modifier requires special treatment to correctly use it, I have discussed How to Use Protected Members Correctly in separate article.


Post a Comment