• 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

Head First Java Inheritance and Polymorphism

 
Ranch Hand
Posts: 36
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I`m reading the book, but currently stumbled upon a problem. I always write problems on paper first and then put it to test in the computer, but when i finished and checked the answer it was other than my answers.
The first set should print :B's m1, A's m2 , A's m3 (this is correct)
The second set should print: B`s m1, A's m2 , C`s m3, 13 (correct again)
The third set should print: A`s m1, A`s m2, C`s m3, 13(correct)
The last set should print: B`s m1, A's m2 , C`s m3, 13 , but the book says it should print: B`s m1, C`s m2, A`s m3
Is it my fault, or is the result in the book wrong?
Thanks in advance.



 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since a2 is an instance of C the result should be the same as the second test. It looks like the book has a mistake. Have you checked the errata to see if this has already been noticed?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which page is that example on? Is it Head First Java™, 2nd edition? I couldn't find it in my polymorphism chapter.
 
Ranch Hand
Posts: 110
Google Web Toolkit Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mihai,

Most of the technical books and mainly those that are translated to another language carry lots of translation bugs, if it's your case then i recommend you to download/buy the english version of it and look at the same place, i had lot of problems with some books and it was because the translation was wrong.

About the Inheritance and Polymorphism there are some tips that i can give you to understand it easier, You must think that the type (variable) is your interface, even if a car have a BMW engine if it doesn't have an interface to access all features of BMW engine it will not be used, and even you driving a car that isn't a BMW it will use all BMW engine(the acceleration is made in a subaru but the behavior is like a BMW) features available on the interface. And it occurs naturaly if you pay attention to the Inheritance tree and the type of the variable, a "top-level" object can have it's own implementations of any method that is already implemented in it's super classes (aka overriding) but it will just call that method if the object is really of his own type, otherwise it will call the version of the real object respecting the interface contract (modifiers included).

 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,
Thank you for your fast answers. My book is actually translated, but i checked both the original book and both have the same problem. The page is 192(in the 2nd edition), i just finished checking the errata and it isn`t posted there, i`ll see how i can post this error so others don`t get confused.
Thank you once more!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It will be several hours before I can look at page 192.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have copied the following classes from the book with slight changes, and, like you, written a Mixed2 class which incorporates all the options. The line before the options tells you what to expect.The output is

campbell@queeg:~/java/page192$ java Mixed2
Top block: expect B A A
B's m1, A's m2, A's m3,

2nd block: expect B A C 13
B's m1, A's m2, C's m3, 13

3rd block: expect A A C 13
A's m1, A's m2, C's m3, 13

4th block: expect B A C 13
B's m1, A's m2, C's m3, 13

The error is not on page 192, but on page 195. There is a position error in the oblique lines. The upper pair of oblique lines show the } and the line correctly positioned on the left. The third oblique line, however, has its } about 5/16inch or 8mm below the code it refers to. Its right end is about 5/16inch or 8mm below the correct answer (A's m1, A's m2, C's m3, 13). The 4th line lacks a } and is also 5/16inch or 8mm below the code it refers to on the left. If you draw a new line parallel to that, but 5/16inch or 8mm higher, it will point to the last block of three lines on the left and to B's m1, A's m2, C's m3, 13 on the right. The correct answer was specified, but the line pointing to that answer has been printed out of register with the rest of the page. That is the erratum you ought to be reporting.
 
Jayr Motta
Ranch Hand
Posts: 110
Google Web Toolkit Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, good one Sheriff .. we need you to keep the law in it's right place! lolol .. i'm joking .. Admirable job paying attention and answering!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

But I would never have done it if you hadn't brought up the question in the first place.

By the way: please post a link to the errata for HFJ; I couldn't find it yesterday.
 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://tinyurl.com/6xbh3cv

here it is.
The code is indeed correct, the answer is wrong

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It is an error between submitting the diagram with the correct answer and its being printed. I believe that diagram (page 195) was submitted showing the correct answers and has been printed incorrectly.

I presume you are submitting that erratum?
 
Mihai Lita
Ranch Hand
Posts: 36
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i will when i`ll have the time. Atm i`m at work so not much time to spare.
 
reply
    Bookmark Topic Watch Topic
  • New Topic