1,105,633 Community Members

Protected access modifier in Java

Member Avatar
pratz
Newbie Poster
9 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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.

Member Avatar
pratz
Newbie Poster
9 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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

Member Avatar
pratz
Newbie Poster
9 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article