I am trying out the following code from Ivor Horton's Java book in which a two dimensional point is implemented via a simple class.
public class Point
{
//x and y coordinates
private xVal,yVal;
//Constructor
public Point (double x, double y)
{
this.xVal = x;
this.yVal = y;
}
//Constructor
public Point (final Point aPoint)
{
this.xVal = aPoint.xVal;
this.yVal = aPoint.yVal;
}
}
Now, the point I don't understand is that in the second constructor which takes an object of type Point as argument, the newly created Point object can access the instance variables x and y of argument Point object directly. This means that private members(methods and variables) of an object can be accessed from inside methods of another object of the same type, in addition to the methods inside the same class. Can anyone please clarify the issue because according to my understanding, the variables of the arugment object should be accessed via getter and setter because they are private.