• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clarification on sample test question for Java SE 8 Programmer I  RSS feed

 
Andy Taylor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took a sample test from the OCA study guide.  This question was presented below.  Can someone explain the answer to me?  The answer is C, E and F.  I don't understand why answer E is correct.  The explanation is that "Line 9 does not compile because the variable reference is to a Frog".  I still don't understand this because ribbit is protected.  How is it that referencing Tadpole as a Frog on line 8 causes this compile issue?  When I code this and put the 2 classes in the same package, every line compiles, including line 10.  I'm stumped.  Was wondering if someone else could elaborate on this?  It seems to me that even through the Tadpole is referenced as a Frog, whether I'm referencing a Frog or a Tadpole, it feels like I should have access to a protected method at that point.  Why not?

Which lines in Tadpole give a compiler error? (Choose all that apply)

1: package animal;
2: public class Frog {
3:   protected void ribbit() { }
4:   void jump() { }
5: }

1: package other;
2: import animal.*;
3: public class Tadpole extends Frog {
4:   public static void main(String[] args) {
5:     Tadpole t = new Tadpole();
6:     t.ribbit();
7:     t.jump();
8:     Frog f = new Tadpole();
9:     f.ribbit();
10:    f.jump();
11:   } }

A. 5
B. 6
C. 7
D. 8
E. 9
F. 10
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Did you try putting them in different packages as the code implies? Frog should be in a package named animal and Tadpole should be in a package named other.
 
Andy Taylor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did code this and validated that the answer they gave was correct.  It doesn't compile.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with those parts of Tadpole that are not involved in the implementation of Frog.  See https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2 (caveat: the JLS can be a little difficult to read)
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us which study guide you mean.

...and welcome to the Ranch
 
Andy Taylor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The study guide I'm using is at https://sybextestbanks.wiley.com/
Narrow the scope to Oracle on that site and this particular question is in the 3rd test exam provide with this book:

OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide:
Exam 1Z0-808


Jeanne Boyarsky, Scott Selikoff


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Frog class is in a different package and the Tadpole class is referencing it. This is a problem. If a Tadpole object was calling the code it would work through inheritance. But here, the Frog call might as well be an unrelated class in a different package.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!