Hello,
If I am not wrong then the method with protected access modifier can be accessed by the same package and the sub-class outside the package. But I have problem regarding the same
I have two packages p1 and p2, but when I try to access the method (protected) of package p1 from package p2, the compilers gives an error that it is not visible or it is protected. I am using Eclipse IDE, even I have checked without using Eclipse, I can not access the method from package p1
Here is the first package p1 and class ClassTest1 with method DisplayName1
package p1;
public class ClassTest1
{
protected void display1()
{
System.out.println("This is ClassTest1");
}
}
Here is the second package p2 with two classes ClassTest2 and MainTest. ClassTest2 has method DisplayName2.
package p2;
import p1.*;
public class ClassTest2 extends ClassTest1
{
protected void display2()
{
System.out.println("This is ClassTest2");
}
}
And
package p2;
public class MainTest
{
public static void main(String[] args)
{
ClassTest2 ct = new ClassTest2();
ct.display2();
ct.display1(); //i get an error on this line, the method display1 is not visible to the class
}
}
Any help is appreciated. Thank you.