Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compile or not?

 
Greenhorn
Posts: 12
  • 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());
}
}
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!
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shirley:
Try this code. May be this make it clear:

[ September 18, 2003: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
shirley tao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic