Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • 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.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thank you all 3.

Matt

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • 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.
 
S.R Paul
Ranch Hand
Posts: 30
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic