• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
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).]
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic