File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile or not?" Watch "Compile or not?" New topic
Author

Compile or not?

shirley tao
Greenhorn

Joined: Sep 07, 2003
Posts: 12
//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());
}
}
What will be the output of compiling and running Class B?
The answer is :
it won't compile.
Who can explain it to me?
Thanks!
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
A.java won't compile because, the line
has a syntax error
(I guess that was just a typo)
B.java won't compile because you are trying to access the variable i as if it is a public variable in class A, where as it is actually a protected variable.


Mani
Quaerendo Invenietis
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
But the more appropriate answer for this question is
Did you give it a try? What happened? What error you got?
[ September 18, 2003: Message edited by: Mani Ram ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Shirley:
Try this code. May be this make it clear:

[ September 18, 2003: Message edited by: Barkat Mardhani ]
Ed Tse
Ranch Hand

Joined: Sep 18, 2003
Posts: 183
Originally posted by Mani Ram:
A.java won't compile because, the line
has a syntax error
(I guess that was just a typo)
B.java won't compile because you are trying to access the variable i as if it is a public variable in class A, where as it is actually a protected variable.

You are right that the code is trying to access the variable as if it is public. However I also tried if class A & B are in the same package, calling the protected attribute, b.i, is fine.


SCJP, Pre-SCJD (URLyBird 1.3.1), Teradata Cert'd Prof
shirley tao
Greenhorn

Joined: Sep 07, 2003
Posts: 12
You are right that the code is trying to access the variable as if it is public. However I also tried if class A & B are in the same package, calling the protected attribute, b.i, is fine.[/QB]
I am a little confused. "i" is a protected variable in A, why can't be accesed from B just because it is not in the same package?
Dep Joy
Greenhorn

Joined: May 07, 2003
Posts: 20
One more thing, class A is defined as
   public Class A
           ^
            should be "class", therefore won't compile
[ September 18, 2003: Message edited by: Dep Joy ]
[ September 18, 2003: Message edited by: Dep Joy ]
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
Hi Shirley,
U can access the protected variable in any of the sub class, either the class in the same package, or in any other package. U can access it as "i" or "super.i".
This was discussed earlier in this LINK
Hope this helps U,
Uma....
shirely tao
Greenhorn

Joined: Sep 07, 2003
Posts: 3
Originally posted by Uma Balu:
Hi Shirley,
U can access the protected variable in any of the sub class, either the class in the same package, or in any other package. U can access it as "i" or "super.i".
This was discussed earlier in this LINK
Hope this helps U,
Uma....

Thank you very much! This link is very good.
I think I misunderstood the meaning of "protected".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile or not?