aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes jplus question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "jplus question" Watch "jplus question" New topic
Author

jplus question

Yuki Cho
Ranch Hand

Joined: Apr 18, 2001
Posts: 31
hi there,
can you please explain to me why the following code doesn't compile? i thought class B is able to access class A since B extends A even though they are in different package.
--------
//in file A.java
package p1;
public class A
{
protected int i =10;
public int getI() {return i;}
}
//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String args[])
{
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}

thanks,
yuki
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by Yuki Cho:
hi there,
can you please explain to me why the following code doesn't compile? i thought class B is able to access class A since B extends A even though they are in different package.
--------
//in file A.java
package p1;
public class A
{
protected int i =10;
public int getI() {return i;}
}
//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String args[])
{
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}

thanks,
yuki

Hello Yuki,
The above code violates the following JLS rule:
6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.


Now let's read the rule from your code point of view
Let A be the class in which a protected member i is declared. Access is permitted only within the body of a subclass B of A. In addition, if i denotes an instance field or instance method, then:
If the access is by a qualified name a.i, where a is an ExpressionName, then the access is permitted if and only if the type of the expression a is B or a subclass of B.

What it means is that you can use i as long as it is not qualified by another name (i.e by itself - that means in your code, you can change a.i to i and the compiler will be happy... But that i will be the inheritted i not the i in class A). If you want to qualify i with another name, that name has to be of the subclass and those subclasses extended below.

Hope that helps,
- Lam -

[This message has been edited by Lam Thai (edited May 13, 2001).]
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Lam,
You have correctly answered the question.
Good explanation.
Ravindra Mohan
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by Ravindra Mohan:
Hi Lam,
You have correctly answered the question.
Good explanation.
Ravindra Mohan

Ravindra,
Thank for being a good cheer leader!
- Lam -
Yuki Cho
Ranch Hand

Joined: Apr 18, 2001
Posts: 31
hi Lam,
i think i got it now. It took some time to sink in.
thanks,
yuki
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jplus question