Protected class members in Java

We know that, protected class members (attributes or methods) can be accessed within the declared class and same package. And through subclas from a different package.

Let's see few examples:

Example 1:

In this example, we see protected members can be accessed within decalared class and same package or not.

Create a class Person.java in the com.company package in the the src folder. It contains a protected void method called printMessage(); and main method too.


package com.company; 

public class Person{
  //protected method
  protected void printMessage(){
    System.out.println("Protected members can be accessible within the decalared class and same package.");
  }
  
  //nain method
  public static void main (String[] args) {
    //create an object of Person class
    Person obj = new Person (); 
    obj.printMessage();
  }
}

If you now run the Person.java class, you will get the following output.

Protected members can be accessible within the decalared class and same package.

Example 2:

Let's see now, protected members can be accessible within the same package but outside the decalared class (means, from a different class)

Create another class called TestExample.java in com.company package.

Note: please remove the main method from the Person.java class now.

Person.java (modified one)


package com.company; 

public class Person{
  //protected method
  protected void printMessage(){
    System.out.println("Protected members can be accessible within the same package  but outside the decalared class.");
  }
}

You see, there is no main method in the Person.java class.

TestExample.java


package com.company;

public class TestExample{
  public static void main (String[] args) {
    //create an object of Person
    Person obj = new Person();
    obj.printMessage();
  }
}

If you now, execute the TestExample.java class, you see the following output in the console.

Protected members can be accessible within the same package but outside the decalared class.

So, we can now assure that, protected members can be accessible within the same package but outside the declared class.

Example 3:

In this example, we try to execute our protected members from a different package. (Remember, it is not possible without creating sub classes)

Create a Person.java class in com.company.person package.

Person.java


package com.company.person; 

public class Person{
  //protected method
  protected void printMessage(){
    System.out.println("Protected members can be accessible within the same package  but outside the package can't be possible without subclass.");
  }
}

Now create another class called TestExample.java in com.company.test package; (or copy paste the previous one in a new package)

TestExample.java


package com.company.test;

public class TestExample{
  public static void main (String[] args) {
    //create an object of Person
    Person obj = new Person();
    obj.printMessage();
  }
}

If you now run the TestExample.java class, you definitely get the following error message.

printMessage() has protected access in com.company.person.Person class.

It means that, protected members can't be accessed from a different package (little modification required)

Okay, we know that protected members can also be accessed outside the package but through subclass. ( Through inheritance actually)

Let's modify the above TestExample.java class. Now it extends the Person class, thus it acts as a subclass.

TestExample.java (modified one)


package com.company.test;

public class TestExample extends Person {
  public static void main (String[] args) {
    //create an object of Person
    TestExample obj = new TestExample();
    obj.printMessage();
  }
}

If you again, run the TestExample.java class, you definitely get the following output now, in your console.


Protected members can be accessible within the same package  but outside the package can't be possible without subclass.