Accessing Protected Members in Java
Last Updated :
24 Sep, 2021
In Java, there are four types of access modifiers. These are public, private, default, and protected. To get the idea of these modifiers, you can refer to access modifiers in java. In this article, we discuss the accessibility of protected members in different cases.
Now let us discuss various scenarios of accessing protected members which are listed below as follows:
- Accessing in the same class
- Accessing in other classes of the same package
- Accessing protected members of a class in its subclass in the same package
- Accessing another class in a different package
- Accessing in sub-class in a different package
Case 1: Accessing protected members in the same class
We can access protected members of a class anywhere in it.
Example:
Java
class Sample {
protected int year = 2021 ;
protected void printYear()
{
System.out.println( "Its " + year + " !!" );
}
public static void main(String[] args)
{
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
|
Case 2: Accessing protected members in other classes of the same package
We can access protected members of a class in another class that is present in the same package.
Java
class Sample {
protected int year = 2021 ;
protected void printYear() {
System.out.println( "Its " +year+ " !!" );
}
}
public class Test {
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
|
Case 3: Accessing protected members of a class in its subclass in the same package
We can access protected members of a class in its subclass if both are present in the same package.
Example
Java
class Sample {
static protected String title = "geekforgeeks" ;
protected int year = 2021 ;
protected void printYear() {
System.out.println( "Its " +year+ " !!" );
}
}
public class Test extends Sample {
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}
|
Output2021
Its 2021 !!
geekforgeeks
Case 4: Accessing protected members in another class in a different package
We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.
Example 1: Package 1
Java
package package1;
public class Sample {
static protected String title = "geeksforgeeks" ;
protected int year = 2021 ;
protected void printYear() {
System.out.println( "Its " +year+ " !!" );
}
}
|
Example 2: Package 2
Java
package package2;
import package1.Sample;
public class Test {
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}
|
Output:
error: year has protected access in Sample
System.out.println(sample.year);
^
error: printYear() has protected access in Sample
sample.printYear();
^
error: title has protected access in Sample
System.out.println(Sample.title);
^
It will give a compile-time error. In the following example, we will create two classes. Sample class in package1 and Test class in package2 and try to access protected members of Sample class in Test class. It is justified in the above two examples.
Case 5: Accessing protected members in sub-class in a different package
We can access protected members of a class in its subclass present in a different package. In the following example, we will create two classes. Sample class in package1 and Child class in package2. Child class extends Sample class.
Example 1.1
Java
package package1;
public class Sample {
static protected String title = "geeksforgeeks" ;
protected int year = 2021 ;
protected void printYear()
{
System.out.println( "Its " + year + " !!" );
}
}
|
Example 1.2
Java
package package2;
import package1.Sample;
public class Child extends Sample {
void helper()
{
System.out.println(year);
printYear();
System.out.println(Sample.title);
}
public static void main(String[] args)
{
Child child = new Child();
child.helper();
}
}
|
Output
2021
Its 2021 !!
geeksforgeeks
Note: From the above output it can be perceived we have successfully accessed the protected members directly as these are inherited by the Child class and can be accessed without using any reference. The protected members are inherited by the child classes and can access them as its own members. But we can’t access these members using the reference of the parent class. We can access protected members only by using child class reference.
Example 2
Java
package package2;
import package1.Sample;
public class Child extends Sample {
void helper()
{
Child myself = new Child();
System.out.println(Sample.title);
System.out.println(year);
System.out.println(myself.year);
printYear();
myself.printYear();
Sample sample = new Sample();
Sample child = new Child();
System.out.println(sample.year);
sample.printYear();
child.printYear();
}
public static void main(String[] args)
{
Child child = new Child();
child.helper();
}
}
|
Output
error: year has protected access in Sample
System.out.println(sample.year);
^
error: printYear() has protected access in Sample
sample.printYear();
^
error: printYear() has protected access in Sample
child.printYear();
^
So the main difference between default access modifiers and the protected modifier is that default members are accessible only in the current package. While protected members can be accessed anywhere in the same package and outside package only in its child class and using the child class’s reference variable only, not on the reference variable of the parent class. We can’t access protected members using the parent class’s reference.
Similar Reads
Protected vs Final Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
5 min read
Protected vs Package Access Modifiers in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
4 min read
Protected vs Private Access Modifiers in Java
Access modifiers are those elements in code that determine the scope for that variable. As we know there are three access modifiers available namely public, protected, and private. Let us see the differences between Protected and Private access modifiers. Access Modifier 1: Protected The methods or
2 min read
Public vs Protected Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
4 min read
Private vs Protected vs Final Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
5 min read
Private vs Final Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using
3 min read
Non-Access Modifiers in Java
Modifiers are specific keywords present in Java using which we can make changes to the characteristics of a variable, method, or class and limit its scope. Java programming language has a rich set of Modifiers. Modifiers in Java are divided into two types - Access Modifiers and Non-Access modifiers.
12 min read
Accessing Grandparentâs member in Java using super
Directly accessing Grandparent's member in Java: Predict the output of the following Java program. Java Code // filename Main.java class Grandparent { public void Print() { System.out.println("Grandparent's Print()"); } } class Parent extends Grandparent { public void Print() { System.out.
2 min read
Protected Keyword in Java with Examples
Access modifiers in Java help to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in java. The access of various modifiers can be seen in the following table below as follows:Â The protected keyword in Java refers to one
5 min read
Public vs Private Access Modifiers in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by usi
3 min read