0
package my.examples.javaexam;

import accessTest.*;

public class DiffPackageSubClass extends Test {
    public static void main(String[] args) {
        Test t1 = new Test();
        System.out.println(t1.a);
        t1.display();

    }
}

package accessTest;

public class Test {
    public int a = 10;
    protected int b = 120;
    protected void display(){
        System.out.println("Hello");
    }
    int c = 130;
    private int d = 999;
}

Hello.

I am trying to figure how Access modifier 'Protected' works.

I've created two packages 'my.examples.javaexam' and 'accessTest'

As far as I know, you can use protected variables or methods if the class is in different package but is a sub class.

So I made DiffPackageSubClass class in different package but made it inherit Test Class in accessTest package.

However, when I try to execute the code it gives me an error saying that the t1.display(); can not be excuted as 'display()' has protected access in 'accessTest.Test'

I am unsure where did I go wrong with the code :(

I've been staring at the code for a while but still couldn't figure out why it gives me an error.

please help me to understand this

Andy Min
  • 91
  • 1
  • 6
  • 1
    Hint: all such basic things have been asked here many many times. They are also documented in any good book or tutorial. And: you are expected to do a bit of research prior posting a question. – GhostCat Dec 06 '18 at 08:01

4 Answers4

2

As per https://www.geeksforgeeks.org/access-modifiers-java/ use

//Java program to illustrate 
//protected modifier 
package p2; 
import p1.*; //importing all classes in package p1 

//Class B is subclass of A 
class B extends A 
{   
    public static void main(String args[]) 
    { 
        B obj = new B(); 
        obj.display(); 
    }  

} 

//Java program to illustrate 
//protected modifier 
package p1; 

//Class A 
public class A 
{ 
    protected void display() 
    { 
       System.out.println("Test"); 
    } 
} 

i.e you need to instantiate with DiffPackageSubClass class in main because protected has access in different package through subclass.

1

Here's what the Java Language Specification says:

Details on protected Access

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

(emphasis mine)

The main method doesn't define the implementation of an object of type DiffPackageSubClass.

In short, you can access this.a and this.display in instance methods of the subclass. But not the protected members of another object of type Test.

More details follow in the specification. This is the official point of truth. In particular, it says:

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

In addition, if Id denotes an instance field or instance method, then:

If the access is by a qualified name Q.Id or a method reference expression Q :: Id (§15.13), where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

So, in your example, the main method could access t1.a and t1.display if t1 was declared (and was an instance of) DiffPackageSubClass.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Protected means that the method\variable is visible within the same package. An its is visible from the class that extends your class. https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

So that the display() method becames the member of DiffPackageSubClass automatically and you can use it like this:

new DiffPackageSubClass().display();

but still you have to call the method of subclass, not superclass.

new Test().display()

is still accessible within the accessTest package only

Akceptor
  • 1,914
  • 3
  • 26
  • 31
0

It should be access from the inherited object, not as a normal object. see example below:

public class DiffPackageSubClass extends Test {
    public static void main(String[] args) {
        Test t1 = new Test();
        System.out.println(t1.a);
        t1.display(); // error
        DiffPackageSubClass diffPackageSubClass = new DiffPackageSubClass();
        diffPackageSubClass.display(); // ok 

    }
}
exudong
  • 366
  • 3
  • 13