• 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

package question

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(�x = � + x);
5. }
6. }
What is the result?
A. x = 0
B. x = 42
C. Compilation fails because of an error in line 2 of class Test2.
D. Compilation fails because of an error in line 3 of class Test1.
E. Compilation fails because of an error in line 4 of class Test2.

The answer for this given as C. Can anyone explain why.

I thought it should work fine since the whole path of Test1 is used when extending it.

Regards,
JP
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try compiling the code. What compilation error do you get? You'll find the answer is not 'C'.

(Hint: if no access level is specified the default is package private.)
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep, the member is default which is "package only". I tried it out.
Change the int x in Test1 to "protected" and it works fine..
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes tht's true the default visibility is within the package and changing the modifier of x to protected or public should work fine. But the problem is then the error should occur at line 4 in Test2, hence the answer should be E. Isn't it?
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK got it , knew that only protected memebers can be accessed (only via inheritance) across packages...but didn't read between the lines..

Thanks for bringing it to light !!

Cheers,
JP
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes the compiler should flag an error at this line...

System.out.println("x = " + x);

since x is not visible here.

Regards
reply
    Bookmark Topic Watch Topic
  • New Topic