• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

protected modifier

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

I have a question regarding protected modifier.
referring to Kathy and Bert book on Sun java cert prog, on page 79,80.

I attached the paragraph from page 79 and 80.
But there is still one more issue we haven;t looked at..what does a protected member look like to other classes trying to use the subclass-outside-the-package to get the subclass' inherited protected superclass member? FOr example, using our previous Parent/Child classes, what happens if some other class--Neighbour, say in the same package as the Child(subclass), has a reference to a Child instance and want to access the member variable x? In other words, how does that protected member behave once the subclass has inherited it? Does it maintain its protected status, such that classes in the Child's package can see it?
No! Once the subclass-outisde-the-package inherits the protected member, that member(as inherited by the subclass) becomes private to any code outside the subclass. So if class Neighbour instantiates a Child object, then even if class Neighbour is in the same package as class Child, class Neighbour won't have access to the Child inherited(but protected) variable x. The button line : when a subclass-outside-the package inherits the protected member, the member is essentially private inside the subclass, such that only the subclass own code can access it.

package certification;
public class Parent{
protected int x = 9;
}

package other;
import certification.Parent;
class Child extends Parent{
public void test(){
System.out.println(x);
}
}

package other; // Neighbour and Child class are located in the same package
class Neighbour {
static public void main(String[] ar){
// create a reference to a Child Object
Child c = new Child();

System.out.println(c.x); // (1) this is what i don;t understand
// the value is still accessible from this class

}

}

Note: the code are in different files.

It is contradict with what the book said, the protected variable x still accessible from Neighbour class.

quote:
--------------------------------------------------------------------------
"So if class Neighbour instantiates a Child object, then even if class Neighbour is in the same package as class Child, class Neighbour won't have access to the Child inherited(but protected) variable x"
-------------------------------------------------------------------------
I think i misunderstood the concept, could any one of you help me to solve the problem.

Sorry if my question is not to the point.


thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried compiling that?
 
Beny Na
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i did,

it was my false, i still have a parent.class in the same package with child and neighbour, so it is still compile.
after i delete the parent.class, it is not working any more, variable x is not recognize by neighbour class.problem solve now.
sorry about that...

thanks
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic