• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from JQPlus

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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() );
}
}
Output : The code will not compile
Can someone explain me the code specially the main() method
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 http://www.javaranch.com/ubb/Forum24/HTML/010329.html
and http://www.javaranch.com/ubb/Forum24/HTML/010698.html
The second one is the same example as you quoted

[This message has been edited by Anshul Manisha (edited July 11, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic