• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Referencing a Parent variable from a Child subclass in a different package

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The origin of this question is a Whizlabs SCJP mock exam question.

The following doesn't compile which I think has something to do with the variable x having been inherited by Child.



But if you put Parent in the same package as Child, there is no compiler error.

Why is that?

Thanks.

Matt
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Protected access gives visibility to other classes in the same package, and to extending classes by virtue of inheritance. This means if we are outside that package and try to see the protected member with a Parent reference, we won't find it. We can modify the main method in your example to access x without error: Here, x is visible because we are using a Child reference to look at it.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. That's how they made it. Protected members are available only form a) code in the same package and b) code that is inside the body of subclasses. Static methods are more or less considered to be outside the body of the class that implements them, but are inside the package of the class that implements them. So, when you have the child class in the same package as the parent, the static function has access to protected member because it is in the same package as the parent. However, when you move the child class you lose access because you are either in the class nor the package.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the exact wording see section 6.6.2 of the JLS...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

The exact wording is...

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object



Since the "implementation" is for the child class (and it doesn't matter if it is an instance or static method implementation), the access must be from something that IS-A child class. This could be directly via the this variable (for instance methods), or from an instance that IS-A child class -- which also includes subclasses of the child class (but obviously, not the parent).

Henry

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

Jayesh A Lalwani wrote:Yeah. That's how they made it. Protected members are available only form a) code in the same package and b) code that is inside the body of subclasses. Static methods are more or less considered to be outside the body of the class that implements them, but are inside the package of the class that implements them. So, when you have the child class in the same package as the parent, the static function has access to protected member because it is in the same package as the parent. However, when you move the child class you lose access because you are either in the class nor the package.


I think this is a little bit misleading because it seems to imply we would be able to write a method in the class Child like the following:

Here we are in the body of Child, nonetheless we can't access Parent.x; we can only see x through inheritance, in a subclass reference:

 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all 3.

Matt

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to KB, a child class outside its parent's package can only access the protected variable through inheritance , not through reference such as p.x.
 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, in other words, even you are inside the child class, you cannot access a protected member of the parent via object reference of parent. Can only through inheritance or child reference(again inheritance).
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic