aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Referencing a Parent variable from a Child subclass in a different package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Referencing a Parent variable from a Child subclass in a different package" Watch "Referencing a Parent variable from a Child subclass in a different package" New topic
Author

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

matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 67
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
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2447
    
  28

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Jan 25, 2010
Posts: 67
Thank you all 3.

Matt

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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.
S.R Paul
Ranch Hand

Joined: Mar 22, 2012
Posts: 30

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).


=-- Anything less than immortality is a complete waste of time --=
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing a Parent variable from a Child subclass in a different package