aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java Inheritance and Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Inheritance and Polymorphism" Watch "Head First Java Inheritance and Polymorphism" New topic
Author

Head First Java Inheritance and Polymorphism

Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

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.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Which page is that example on? Is it Head First Java™, 2nd edition? I couldn't find it in my polymorphism chapter.
Jayr Motta
Ranch Hand

Joined: Jul 30, 2010
Posts: 110

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).



Feel free to ask me anything!
www.BlackBeltFactory.com/ui#!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

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
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Thank you. It will be several hours before I can look at page 192.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jul 30, 2010
Posts: 110

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
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jan 11, 2011
Posts: 36

http://tinyurl.com/6xbh3cv

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jan 11, 2011
Posts: 36

Yes, i will when i`ll have the time. Atm i`m at work so not much time to spare.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java Inheritance and Polymorphism
 
Similar Threads
Abstract inner class?
Protected
Overiding class variable through inheritance
Dans exam
Head first java excercise problem: Could not find main class