• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question from JQPlus

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
this is supposed to be a surprise, but it smells like a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic