Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Different packages and inheritance

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, here is another question I'm not too sure about one of the answers (taken from Mock exam 3 on http://sybextestbanks.wiley.com/)

Which line in Tadpole give a compiler error?

A 5
B 6
C 7
D 8
E 9
F 10


I got two right, C and F but I didn't get E.

The explanation says about line 9:
Line 9 doesn't compile because the variable reference is to a Frog.


Still not sure I understand why it doesn't compile though. Can it not call because the variable reference is to a Frog? ould I possibly ask for a more detailed explanation please?

cheers
 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll find an excellent explanation about the same question in this topic.
 
Henry Wong
author
Sheriff
Posts: 22610
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was already explained in one of your earlier topics...

https://coderanch.com/t/669691/java/packages-protected-access

Henry
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I understand why it doesn't compile though. Can it not call
?
1
f.ribbit()
because the variable reference is to a Frog?


Yes. Your understanding is right. Frog, Tadpole are in different packages. Tadpole is extending Frog.  From Tadpole , you can access Frog members using Tadpole reference variable. You can't access using Frog  reference variable because Frog methods are protected , default which doesn't have scope out side the package. protected members are accessible through Sub class reference variable in other package.
You try to create object for Frog class and access Frog methods through that reference variable.Compiler will give errors. Here also same concept applies.

Hope this helps !
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, it's cleare now.
This was already explained in one of your earlier topics...
I missed that, sorry!
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!