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

Confusion in Polymorhphism Self test questions K&B Book

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help me understand why I got these wrong:



I chose D but the correct answer is F. I guess I don't understand how you can tell from just looking at the code that it will generate a compilation error, or that it will generate a runtime exception.

I guess I could have compiled the code and tried to run it - but I don't think you'll be able to do that on the actual exam.
 
Tod Stroszer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I chose D but the correct answer is A.

This is another case where I understood the basic cause of the error related to casting and trying to use an incompatable type in the go2 method. But again I'm not sure how you can tell just from looking at the code why a runtime exception is thrown and not a compilation at line 9.
 
Tod Stroszer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is it better for me to be typing these questions into an IDE and trying to compile them? I'm assuming on the actual exam I'm won't have the ability to use javac, so I'm trying to judge the outcome of code in the sample questions just by reading them.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tod,
3 posts in short time time!
I think you are confusing with actual object and its reference type.

question 1:



your Dog class doestnt have sniff method.

question 2:

this go2(new Tree(), new Redwood()); calls



because Redwood is a Tree but vice-versa is not true.

hth

 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A ClassCastException is a Runtime Exception,which means it won't be caught at compile time. You can cast a Tree object to a Redwood object, the compiler won't recognize it. It'll recognize if you try to cast a JockStrap object to a Redwood object, because a JockStrap can never be cast to a Redwood, but not a Tree, or an Object.


The Dogshow thing is slightly more tricky. Because it is cast up to a Dog reference, the compiler will only register the methods of Dog as possible methods used on that reference.
 
Tod Stroszer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand why there are problems with the code related to casting in both questions. When I was doing the questions I knew that there was a casting problem - my problem is in the choices available for the answers - I'm having trouble judging from just looking at the code when a runtime exception will be thrown and when a compilation error will be thrown.

I know that if I type the code out and try to compile and run I will get the correct answer, but I am trying to figure out what the result is without compiling and running the code - as you won't have javac available to you in the actual exam.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tod Stroszer wrote: but I am trying to figure out what the result is without compiling and running the code - as you won't have javac available to you in the actual exam.


remember that *practice makes you perfect*
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic