• 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

Access Modifier protected -- SCJP Study Guide page 36

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! The code displayed below suggests that note " So if a class Neighbor instantiates a Child object, class Neighbor won't have access to Child's inherited protected variable x. " Can you please tell me why the code below does not hold up? Thank you in advance.

(1) Fruit.java

package food;
public class Fruit
{
protected boolean test() { return true; }
protected int fruitBasket = 3;
} // end of Fruit

(2) Apple.java

package testing;
import food.Fruit;

class Apple extends Fruit // visibility: default/package
{

void getAccessOkay() {

System.out.println( test() ); // inherited protected class member
System.out.println( fruitBasket); // inherited protected class member
}

} // end of Apple

(3) Beef.java

package testing;
class Beef // visibility: default package level -- same levela as Apple.
{
public static void main(String[] args)

{
protectedMethod();

}

public static void protectedMethod () {

System.out.println("inside Beef.java protectedMethod" );
Apple a = new Apple();
a.accessFromNeighbor();
a.getAccessOkay();

}
}// end of Beef.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Portland, Welcome to java ranch. you should write your code using code tags. I am doing it here for you.


and I dont see class Beef accessing any of the protected member of class Fruit directly. Its just calling a method from class Apple which is in the same package, and its class Apple that's accessing the protected members of class Fruit which is legal as Apple extends Fruit.
 
Graciela Zaffarana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again ... I apologize. My typing fingers ran away from me before I was able to pose the question correctly last time I was here.

Let me try again:

The code that I submitted below compiles and runs successfully, suggesting that one can access by reference a protected variable of a Superclass via the Derived class --- even when the Superclass is in a different package from the Derived class and the Neighbor class. In summary, I don't see statement on page 36 of SCJP 6 study guide holding up. That, in turn, reflects "incorrectly" -- in my experience --- on the graph on page 37 --- rightmost and lower entry .

I understand and saw in test code that the Derived class cannot access protected class member in Superclass that resides in different package via a reference; it only can do it via inheritance. UNDERSTOOD. What I do not see is the nuance that is being presented with regards to the "Neighbor class." I hope that this clarifies my question. I greatly appreciate your reply.

So far, the study guide has been very helpful! Thank you! GZ.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly what I dont understand is where in the subclass are you accessing protected members via reference to the superclass ?
and by neighbour class do you mean the Beef class in your code then you are not accessing any protected member of any class in your Beef class.

If still you have any problem, I will try to explain.
Please specify the line numbers for the code lines in which you have problem because I am not able to understand your question.
 
Graciela Zaffarana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. I now see what is going on. Thank you for your input. Best regards.
 
Graciela Zaffarana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Neha ...

I apologize; we keep cris-crossing messages ...

Beef is the class that resides in the same package as Apple. Apple is the derived class. Fruit is the superclass that resides in a different package from Apple and Beef.

In the class Beef, line 43, you can see that via an instance of Apple, the protected superclass member fruitbasked can be accessed; in this case it is a getter but it could have been a setter as well. The book speaks of (D), (I) or (R) accesses on page 37 --- Study Guide 6.

The code that I submitted suggests to me that one can indeed access by reference a protected member in a Superclass from a class that resides in the same package as a derived class, but different fromt the superclass.

Thank you for your input and patience.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well what the statement says is you can't directly access the protected member.
You are using a reference of subclass to access subclass's getter/setter method not the superclass reference to access its protected member.
The only class that is directly accessing the protected members is the subclass and that also through inheritence not through reference to superclass.

That line in book means you can only access the protected members of a class from a class that is in different package only if the class from different package extends it.
And if you want to access the protected members from a subclass in different package it should be through inheritence not by the reference to super class.

and the other classes in the same package as the subclass cannot access directly the protected members of the super class by anyways not even by reference to super class if you want to access those protected members the only way is to use getter/setter methods of subclass or superclass and for that you will need reference to that class
 
Graciela Zaffarana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Neha ...

Okay; we are on the same page now. Thank you so much for taking the time to follow through my question. I greatly appreciate your input. Best regards, GZ!
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic