• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

protected variable?

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


What will be the output of compiling and running class B ?
Answer : It will compile
According to me it should compile because as 'i' is declared as protected in class A, Class B can access this variable?
Please explain
Sonir
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will give compiler error because i is protected. And you can access the code in subclass only by subclass reference. You can't access from superclass reference.
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arathi
I am still not clear with the concept, could you just explain me in a bit detail..If possible with examples?
Sonir
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure you have the right code here:
{a.i = a.i*2;} is this ok ? can u verify this again. this assignment itself looks vague. we are not even discussing protected or etc. just the assignment. the RHS looks like some typo error.

Originally posted by sonir shah:
Consider following two classes:


What will be the output of compiling and running class B ?
Answer : It will compile
According to me it should compile because as 'i' is declared as protected in class A, Class B can access this variable?
Please explain
Sonir

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

Originally posted by mark stone:
are you sure you have the right code here:
{a.i = a.i*2;} is this ok ? can u verify this again. this assignment itself looks vague. we are not even discussing protected or etc. just the assignment. the RHS looks like some typo error.



Why does this look vague to you? Even out of context, this assignement is just saying "take the member i in the object referenced by variable a, and multiply this by two, then store it back into the memory associated with data member i in the object referenced by variable a."
It's the same type of construct as
i = i*2.
Where are you confused?

Rob
[ January 17, 2002: Message edited by: Rob Ross ]
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonir,
I think your orginial post about this code is here. Titled
"Why isn't compiling" a few people have answered your question. And I think the answer are pretty good. If you write another class called C.java and put it in the same package as A.java you'll see it can gain access (just like what Prashanth menon and Rob Ross
say).
hope this helps.
Bob
[ January 17, 2002: Message edited by: Bob Tai ]
[ January 17, 2002: Message edited by: Bob Tai ]
 
Quick! Before anybody notices! Cover it up with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic