File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from JQPlus 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 "Question from JQPlus" Watch "Question from JQPlus" New topic

Question from JQPlus

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
// in file
package p1
public class A
protected int i=10;
public int getI() { return i ;}
// in file
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() );
Output : The code will not compile
Can someone explain me the code specially the main() method
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Angela the same example has been discussed within last week on this forum. The reason the code will not compile is because you are trying to access a protected variable by the code which is not responsible for the implementation of that object. So even though class B extends A - B cannot access protected variables of A in the code which is not responsible for implementing the object.
check out the following discussion
The second one is the same example as you quoted

[This message has been edited by Anshul Manisha (edited July 11, 2001).]

AM<BR> <A HREF="" rel="nofollow"></A>
I agree. Here's the link:
subject: Question from JQPlus
It's not a secret anymore!