• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

accessing a protected access instance variable defined at a superclass

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All

I am preparing myself for the Certification exam, I am going to have it in 1 week and half. So while studying using the SCPJ 6 Study guide book, I stumbled on the following test sample that confuse me.

In chapter 10 the following test is



What is the result? (Choose all that apply.)
A. 5 6 5 6
B. 5 6 followed by an exception
C. Compilation fails with an error on line 6
D. Compilation fails with an error on line 7
E. Compilation fails with an error on line 8
F. Compilation fails with an error on line 9

The correct Answers are: C, D and E are correct. Variable a (default access) cannot be accessed from outside the package. Since variable b is protected, it can be accessed only through inheritance.

The wrong answers are: A, B, and F are incorrect based on the above. (Objectives 1.1, 7.1)

Now the doubt: I am really feeling frustated because this is strange behaviour, my understanding was based on the explanation provided on chapter 1 page 32 to 36.
Why F is not included (I run the test on the computer and it does not consider as an error condition)

Please could you explain me why "new Fiz().b" does not thrown a compilation error as "new Foo().b" does. For me I can not find the logic behind.

Thank for any help provided.



 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Protected instance variables can be accessed, BUT only through the inheritance (if they are accessed from within a class which is in other package).
Let's follow that logic:
1). You instantiate the subclass (protected member can be accessed through the inheritance).
2). You can access protected member:
a). anonymously - new SubClass().member;
b). with reference type:
SubClass sc = new SubClass();
sc.member;

YOU ACCESS IT THROUGH THE INHERITANCE - YOU DID NOT DEFINE "member" INSIDE "SubClass" - IT IS INHERITED.

But invoking (from within SubClass):
SuperClass superClass = new SuperClass();
superClass.member; //error
Gives You a compiler error. YOU DO NOT ACCESS "member" THROUGH INHERITANCE.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben, welcome to javaranch.

This type of question has been asked before like here or here. See if that could help...
 
Ruben Guillen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All

Thank you very much for the prompt response. Lukas explanation was clear for me and I complemented with references provided by Ankit.

It is a relief for me to know this information, one less tricky question to solve during the exam.

..And thanks Ankit for the welcome.

Regards.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, and please have look at the protected static variables. They can be a little trickier.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic