• 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

scjp 6 study guide - protected access question

 
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following code is taken from the SCJP 6 KB study guide. I cannot seem to understand the theory behind the line that will not compile in this code (as mentioned in the comment). I thought that protected variables could be accessed outside the package via inheritance but something about the polymorphic reference voids this access. Can someone explain this behavior.


--------------------------
package foo;
public class Parent {
protected String pa = "vader";
protected String getPa() {
return pa;
}
protected void setPa(String pa) {
this.pa = pa;
}
}
---------------------------
package foo;
public class Child extends Parent {}
---------------------------
package baa;
import foo.Child;

public class SmallChild extends Child {
public static void main(String... args){
Child kind = new SmallChild();
System.out.println(kind.pa); // WON’T COMPILE

// pa is protected, so it can be used from a subclass of another
// package, but it cannot be used via a reference of a parent class.

SmallChild kindje = new SmallChild();
System.out.println(kindje.pa); // WILL COMPILE
}
}
---------------------------
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I defer to Dennis below for giving a better explained answer
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Asad,

In the case of kindje, it is a SmallChild, and we are inside the SmallChild class. SmallChild has visibility to any protected members of its superclass. So we can access 'pa' through inheritance.

In the case of kind, even though it is instantiated as a SmallChild, it is referenced as a Child. So when we try to access the 'pa' attribute, we are looking from package baa into package foo. Since we are looking into Child from outside the package, we do not see 'pa'.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the case of kind, even though it is instantiated as a SmallChild, it is referenced as a Child.



It seems to me that even if the SmallChild object was referenced as a SmallChild we would still not be able to access 'pa' because the access would still be from the package foo to baa (the line 'kind.pa' is in package foo)?

I am not sure what the SmallChild object being referenced as a Child has to do with the access in this case.

Thanks.
 
Yadrif Rifkins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind. After another look I see what you mean.

Thanks.
 
Greenhorn
Posts: 28
1
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it....
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic