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

Protected and Default members, different packages...Please explain...

 
Boris Mechkov
Ranch Hand
Posts: 72
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all hello to all posters here. I have been reading this forum for 2 years now but just registered because i am studying really hard for the OJCP 6 exam...So thanks to all of you around here who decide to help (and the rest as well)...

This question comes from the K&B Book, Chapter 10 Self Exam questions...In a nutshell:



Can someone explain why the first two Comp. Errors occur (CE)...I thought we are looking at "a" and "b" from a FOO instance, so we should have access to them. (even though the Foo instance is in a different package)...
Forgive if this is obvious but i have read 240 pages from the book for the past 9 hours...

 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, if you're in the Fiz class, you're in the B package, regardless of what you're inheriting from.

So access to a is impossible, and access to b is only possible via inheritance (i.e. you can't access it via a reference to the superclass).
 
Boris Mechkov
Ranch Hand
Posts: 72
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:No, if you're in the Fiz class, you're in the B package, regardless of what you're inheriting from.

So access to a is impossible, and access to b is only possible via inheritance (i.e. you can't access it via a reference to the superclass).


Hmmm, agreed. I was expecting to see something like Foo f= new Fiz(); or Fiz f = new Fiz(); and then it would have made more clear sense.

Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic