• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 9697
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 is no greater crime than stealing somebody's best friend. I miss you tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic