This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from JQPlus" Watch "Question from JQPlus" New topic
Author

Question from JQPlus

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
// 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

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 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).]


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
 
 
subject: Question from JQPlus
 
Similar Threads
please answer the question and explain it to me,thanks!
protected variable?
Compile or not?
why compile time error???
How does this work ?