• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a protected variable be seen through reference from a subclass outside the package?  RSS feed

 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that the answer always NO. But why does this code work and ouput "protected"?


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Class Child is a subclass of class Parent, therefore it can see the protected members in class Parent.

If you try this in a class that is not a subclass of Parent, you'll see that the protected member is not accessible.
 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.

Class Child is a subclass of class Parent, therefore it can see the protected members in class Parent.

If you try this in a class that is not a subclass of Parent, you'll see that the protected member is not accessible.


It can see through inheritance, NOT reference.

Even more if I try this through subclass of the Child class I'll see compilation error.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by "NOT reference", a subclass can access protected members of its super class, regardless of how it is accessed (permission wise that is).

Henry
 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Not sure what you mean by "NOT reference", a subclass can access protected members of its super class, regardless of how it is accessed (permission wise that is).

Henry


From the book "SCJP Sun Certifi ed Programmer for Java 6 Study Guide (Exam 310-065)" by Kathy Sierra and Bert Bates

For a subclass outside the package, the protected member can be accessed only through inheritance.


Take a look to tha attached picture.

This is legal :
protected.png
[Thumbnail for protected.png]
Diagram of the access of protected method
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are implying something that is simply not true. A subclass can access a protected of its super class-- whether it is via the this reference or any other reference.

It is however, required that it's responsible for the instance, so the reference type must be of IS-A subclass.

Henry
 
Jakhongir Raimbekov
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:You are implying something that is simply not true. A subclass can access a protected of its super class-- whether it is via the this reference or any other reference.

It is however, required that it's responsible for the instance, so the reference type must be of IS-A subclass.

Henry


It will not compile

Although, child IS-A Parent.
No matter, probably I cannot understand totally the issue. Thanks for all your answers Henry.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As already mentioned, the code accessing the protected field must be responsible for the instance.

In Your last example, the code accessing the protected field is in the GrandChild class, hence, the reference must be IS-A GrandChild.

Change your instance and reference to GrandChild, and it should work.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!