• 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

Kindly explain this code from SCJP faq

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


The reasoning given it wont compile is :

REASON: Although, class B extends class A and 'i' is a protected member of A, B still cannot access i, (now this is imp) THROUGH A's reference because B is not involved in the implementation of A. Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the implementation of B.

I could not follow th explanation. Why cant A access i even if it is having protected access in B. Even if we change A a = new B() to A a = new A() and then invoke the process method, then also i is not accessible. Why?
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to solve your problem by using a B object with Object variable a.Now to access the newly modified process(B obj) method, you need to cast the 'a' variable to B in order to fulfill the method argument.
'A' object cannot access its protected variable via a . operator external to its package.
Hope it works
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is a similar problem with protected variable,

AS we all know that protected variable can be accessed from a differenct package(through inheritance). the above prg seems like doing so, but there is an err saying that the variable is protected and cant be accessed. why is it so.. please explain..
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi venkatesh,

The problem with your prgm., is protected members of a class are visible to its sub class in different package only through inheritence(and not with object reference)

In your program, 'protected x2' of 'pack' is visible into 'test' only through inheritence. So System.out.println(x2); is valid. but x2 is not a static member. so you may need a non-static method for class test to display x2 as the following


x4 is visible only in class 'pack' as it is private and x3 is not visible into 'test' as it is neither public nor protected.

jagan.k
[ August 15, 2008: Message edited by: jagan kay ]
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jagan
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it would also have been possible had we used the object of test class to refer to x2.

As in

Test t = new Test()
System.out.println(t.x2)

to be put in main.

What say?
 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anand,

Ofcourse, it is possible as any non-static member can be used in a satic context only with an object reference. I simply forgot to point it.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No x2 is not static. The point over here is that x2 being protected can only be accessed in the subclass through inheritance and not by reference. Therefore, since class test has obtained x2 (it being protected) on extending its class, it is accessible through an object of test class only. In test class it would not be allowed to make an object of x2's class and access it through that object. The whole story is in the word protected.
 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anand,

This is what what i clearly told in my first post by definition & by code as well.

in my second post as you told further,

what i thought of pointing out is once the portected variable 'x2' of class 'pack' is available in class 'test'(in different package
only through inheritence not through the object reference of class 'pack') it can be accessed directly from any non-static
method of class 'test' and only by the object reference of class 'test' from inside a static method of class 'test'(as how we do
it[accessing non-static member from inside a static context] in general regardless of 'protected, inheritence in to different package')
since 'x2' is a non-static variable.

here, by "...only with an object reference", i meant the reference varaible of class 'test' not by the one of class 'pack'(i should
have explicitly mentioned it in my second post once again) from inside a static method(like 'main()' as you told in your second post) to access x2(non-static field).

Thanks,
jagan.k
[ August 17, 2008: Message edited by: jagan kay ]
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic