Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

SCJP-K&B technical question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have gone through the protected access modifiers at page 35 of scjp 1.6 written by kathy and bert and i have written a similar code in eclipse an tested it and works not as mentioned in the book .code is as follows .

Vegetable bean is the parent class in the food package is as follows
package food;



Receipe is the child class which is in the package snack



The code works perfectly fine and there are no errors .According to the book the compiler MUST give an error at bean.kilo , since i am accessing the protected variable NOT THROUGH INHERITANCE BUT THROUGH REFERENCE .I AM LOST NOW AND KINDLY CAN YOU CLARIFY .


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

The code works perfectly fine and there are no errors



Your code, as it is, does not give an error on the line 13 of child class.

Your code is not the same as that mentioned in the book. The code in the book discusses protected instance variables , but you have used a static variable "kilo" in the parent class.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go back to Chapter one and read on the section "Access Modifiers (Exam Objectives 1.3 and 1.4)" and take a closer look at Figure 1-4 on protected members access through Inheritance you'll understand why.

In your case you access protected member "kilo" through Inheritance so, even you in different package you still able to access "kilo". See Figure 1-4 for more insight.



You don't need to create a new instance of VegetableBean to access "kilo" member since "kilo" is static and you access it through Inheritance.




Hope that clear up your confusion...
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic