Contiuning Reading Java Language Specification turned to chapter Names.
Discussing more about Protected Fields, Methods, and Constructors.
Can we predict the output of the following program?
package packages_protected_examples.points;
import packages_protected_examples.threePoints.Point3d;
public class Point {
protected int x, y;
void warp(Point3d a) {
if (a.z > 0) // compile-time error: cannot access a.z
a.delta(this);
}
}
package packages_protected_examples.threePoints;
import packages_protected_examples.points.Point;
public class Point3d extends Point {
protected int z;
public void delta(Point p) {
p.x += this.x; // compile-time error: Point.x is not visible.
p.y += this.y; // compile-time error: Point.y is not visible.
}
public void delta3d(Point3d q) {
q.x += this.x;
q.y += this.y;
q.z += this.z;
}
public static void main(String[] args) {
Point3d point3d=new Point3d();
point3d.delta(new Point());
}
}
Reason:
When we closely look into Java Language Specification it states:-
A compile-time error occurs in the method delta here:
it cannot access the protected members x and y of its parameter p, because while Point3d
(the class in which the references to fields x and y occur) is a subclass of
Point (the class in which x and y are declared), it is not involved in the implementation
of a Point (the type of the parameter p). The method delta3d can access the protected
members of its parameter q, because the class Point3d is a subclass of Point
and is involved in the implementation of a Point3d.
The method delta could try to cast (§5.5, §15.16) its parameter to be a Point3d, but
this cast would fail, causing an exception, if the class of p at run time were not Point3d.
A compile-time error also occurs in the method warp: it cannot access the protected member
z of its parameter a, because while the class Point (the class in which the reference to
field z occurs) is involved in the implementation of a Point3d (the type of the parameter a),
it is not a subclass of Point3d (the class in which z is declared).
Now Reading more about Default-Access Fields, Methods, and Constructors section.
Can we predict the output of the program?
package packages.point;
public class Point {
public int x, y;
void move(int dx, int dy) {
System.out.println("Inside Point move() method.");
x += dx; y += dy;
}
public void moveAlso(int dx, int dy) {
move(dx, dy);
}
}
package packages.morePoints;
import packages.point.Point;
public class PlusPoint extends Point {
public int x=80;
public int y=20;
public void move(int dx, int dy) {
System.out.println("Inside PluPoint move");
moveAlso(dx, dy);
}
public static void main(String[] args) {
PlusPoint plusPoint=new PlusPoint();
plusPoint.move(10,30);
System.out.println("x:" + plusPoint.x);
System.out.println("x:" + plusPoint.y);
}
}
Output:
Inside PluPoint move
Inside Point move() method.
x:80
x:20
Well when we read what JLS states:
If none of the access modifiers public, protected, or private are specified, a class member or constructor is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package.
If a public class has a method or constructor with default access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package.
Because move of Point is not overridden by move in PlusPoint, the method moveAlso in Point never calls the method move in PlusPoint.
it terminates normally. If move of Point were overridden by move in PlusPoint, then this program would recurse infinitely, until a StackoverflowError occurred.
Note Also An array type is accessible if and only if its element type is accessible.
Hope this explanation helps if any.
Monday, 1 October 2007
Subscribe to:
Post Comments (Atom)
7 comments:
PLEASE format your code... You can't show a horrible mess of code (which is in itself a convoluted obfuscated example full of variables with single character names), then expect people to know what the output is supposed to be. I know it would require some effort to use a fixed-width font and have whitespace, etc... But for this sort of post, surely it's necessary?
I agree.
That would be useful to your intended audience.
Tim/Fadzlan,
Sincere Apologies for this.Will try to format code in later posts.
Thanks
Prashant
Are you sure that's the output? I would think it's actually "Inside PluPoint move" until you get a stackoverflow.
Come on!
The code is formatted the indian-way!
^^ not all that funny, guess thats why you posted anonymously...
Thanks a lot Mike for encouragment.
Anwering one user query it won't result in StackOverFlow exception as described in the explanation "If a public class has a method or constructor with default access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package"
Post a Comment