Click here to Skip to main content
16,010,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

My question is that:
My project has 2 packages named package1 (with file Circle.java and Test.java inside), package2 (with file Test2.java inside). Class Test2 is subclass of class Circle and Circle has a protected field name z. The situation is like this:

class Circle like this:
Java
package package1;

public class Circle {
	private int x;
	private int y;
	protected int z;
	public Circle()
	{}
}


Class Test2 is like this:
Java
package package2;
import package1.Circle;

public class Test2 extends Circle{
	private Circle c = new Circle(); 
	void modifyCircleObjectState()
	{
		c.z = 20;
	}
}


Eclipse says: The field Circle.z is not visible.
Following the link http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html[^]
I've found that The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Alright, Test2 is a subclass of Circle and it's located in another package (in this case package2). So why from Test2 the z field of object c is not visible.

What is wrong with my thinking?

Any help or suggesstion would be appreciated!
Posted

Well, you are extending Circle here, so you have direct access to z inside Test2. What you can't do is instantiate Circle (here you've done it as c), and try to access a protected field. If you change your code like this
Java
package package2;
import package1.Circle;
public class Test2 extends Circle {
  void modifyCircleObjectState()
  {
    z = 20;
  }
}
As you can see, there is no separate Circle instance - so when somebody instantiates Test2 and calls modifyCircleObjectState, z is updated there.
 
Share this answer
 
You can access the z of your super class but not access z of an instance of Circle. Meaning you can only access the inherited z, but not the z value of any other Circle class.
Compare it like if it was about your own kids. You can access their name value and set it to any name you want, but you can not decide the name of the kids of other people, even though they are descendant of an adult. They simply do not belong to you. This might raise the question why there is no possibility to adopt? In C++ you can declare friend operators which are granted more access but there is no equivalent in java.

Good luck!
 
Share this answer
 
This may clarify solution 2 above. What protected means is that classes that inherit from a class with a protected variable can access that variable (which would not be true for a private variable declared in the parent class). Other classes do not have access to the protected variable.
 
Share this answer
 
Sorry if my comment is going to a wrong place. How do I accept these above solutions? Before I've accepted some the other post of mine.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900