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