Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Clarification on sample test question for Java SE 8 Programmer I

 
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
 
Sheriff
Posts: 15768
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 15768
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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)
 
Marshal
Posts: 69799
277
  • 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


 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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.
 
We noticed he had no friends. So we gave him this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic