This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected acces modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected acces modifier" Watch "protected acces modifier" New topic
Author

protected acces modifier

Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Given the next code in diferent directories:
<pre>
-------- Code 1 ----------------------
package simple;
public class Parent{
protected void method(){
System.out.println("method in Parent");
}
}
-------- Code 2 ----------------------
package complex;
import simple.*;
public class Child extends Parent{
public static void main( String args[] ){
Parent p = new Parent();
p.method();
}
}
-------- Code 3-----------------------
package complex;
import simple.*;
public class Child extends Parent{
public static void main( String args[] ){
Child p = new Child();
p.method();
}
}
-------------------------------------
</pre>
which will result in compilation error?:

complex/Child.java:7: method() has protected access in simple.Parent
p.method();
^
1 error

and which in:

method in Parent

message when executed

Note: Child is only one java file, but one of the codes is the cerrected version.

Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
anyone ?
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
anyone?
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
I think
code t3 is the rt. version of Child.u can acces the inherited version of method.but not Parent's one .
corr me if I am wrong
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
I think
code 3 is the rt. version of Child.u can acces the inherited version of method.but not Parent's one .
corr me if I am wrong
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Thanks Sathi, for your reply ( and only )
This is what I dont understand:
Parent has a protected method called "method", ok?
so protected methods only can be accesed by clases of the same package or subclasses ( in differents packages) but, why in code 2 it turns in compiler error?.
Child is a subclass of Parent, then method should be visible
am I right or wrong??
So the question again is, WHY Parent method "method" cannot be accesed in Child's main if is in the scope??.
The only answer rasonable I've found is this:
Child inherit the method, Child can see it because is a subclass, but main cannot see it because is static and theres no instance there..
Wel Im a lot confused because main is still part of the child definition...
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
.
[This message has been edited by Zkr Ryz (edited May 10, 2001).]
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
.
[This message has been edited by Zkr Ryz (edited May 10, 2001).]
kulbir
Greenhorn

Joined: Feb 08, 2001
Posts: 4
Given the next code in diferent directories:
-------- Code 1 ----------------------package simple;public class Parent{ protected void method(){ System.out.println("method in Parent"); }}-------- Code 2 ----------------------package complex;import simple.*;public class Child extends Parent{ public static void main( String args[] ){ Parent p = new Parent(); p.method(); }}-------- Code 3-----------------------package complex;import simple.*;public class Child extends Parent{ public static void main( String args[] ){ Child p = new Child(); p.method(); }}-------------------------------------
which will result in compilation error?:
complex/Child.java:7: method() has protected access in simple.Parent
p.method();
^
1 error
and which in:
method in Parent
message when executed
Note: Child is only one java file, but one of the codes is the cerrected version.
although you are right that if protected access specifier is
used then that member can only be accessed by outside class(another package)which is a subclass of parent class but
there is another thing to remember that protected members can
only be accessed through reference of child class or any other subclass of this child class but if u try to accesss it through
parent class's reference then it will give compiler error explaining the error in code2.for further study u could
refer to khalid's chapter number4.
i hope this clears ur doubt.
Rahul Ramachandran
Ranch Hand

Joined: Feb 27, 2001
Posts: 48
Hi Zkr,
Please refer to this JLS explanation: http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36191
I have copied it here:
6.6.7 Example: protected Fields, Methods, and Constructors
Consider this example, where the points package declares:
package points;
public class Point {
protected int x, y;
void warp(threePoint.Point3d a) {
if (a.z > 0)// compile-time error: cannot access a.z
a.delta(this);
}
}
and the threePoint package declares:
package threePoint;
import points.Point;
public class Point3d extends Point {
protected int z;
public void delta(Point p) {
p.x += this.x;// compile-time error: cannot access p.x
p.y += this.y;// compile-time error: cannot access p.y
}
public void delta3d(Point3d q) {
q.x += this.x;
q.y += this.y;
q.z += this.z;
}
}
which defines a class Point3d. 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).
Cheers!
Rahul
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Thanks for your answers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected acces modifier
 
Similar Threads
Access to protected variable of class in one package from other package
Doubt in protected modifier
run time error
a problem about overriden
the same instance has different method?