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.

Because it's not. In that example it is the "MainTest" class, not the "ClassTest2" class that is attempting to access the method. It is simply using an instance of ClassTest2 to try and access it, but it is MainTest that is trying to access it. Add a call to "display1" inside the ClassTest2 class and you'll see that ClassTest2 does have access to it.

Hey,
Thanks for that, but still have the same issue, as you said I modified the code, but still I can not access the method from package p1.

Here is the modified code, 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(now modified) and MainTest. ClassTest2 has method DisplayName2.

package p2;
import p1.*;

public class ClassTest2 extends ClassTest1 
{
	protected void display2()
	{
		System.out.println("This is ClassTest2");
		
		ClassTest1 t = new ClassTest1();
		t.display1(); //get a error on this line, the method display1 is not visible to the class
	}
}

And

package p2;
public class MainTest 
{
	public static void main(String[] args) 
	{
		
		ClassTest2 ct = new ClassTest2();
		ct.display2();
		ct.display1(); //get a error on this line, the method display1 is not visible to the class
	}

}

If it is possible for you please compile the code and check it.

Why are you instantiating a ClassTest1 instance in ClassTest2? Simply call "display1()", not "t.display1()".

Simply call "display1()", not "t.display1()".

Thanks man this worked, but the point is that I have not understood that why we can not instantiate an object of the base class here. If I create an object of the base class, that is

ClassTest1 t = new ClassTest1();
t.display1();

and if the display1() is public in base class, the program runs and if I declare the method as protected, then I do not have to create an object and just use the method name. This sounds confusing........ If you have a link or if you can explain me this in details, it would be great. By the way thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.