• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Mixed Message Chapter 7  RSS feed

 
John Dodge
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm currently trying to learn java and my friend gave me an exercise to do and I believe he got this problem off a book called Head First Java. It would be appreciated if some of you more experienced programmers can see if I am doing the exercise correctly.



My answers
b.m1();
c.m2();
a.m3(); = B's m1, A's m2, A's m3,

c.m1();
c.m2();
c.m3(); = B's m1, A's m2, C's m3, 13

a.m1();
b.m2();
c.m3(); = A's m1, A's m2, C's m3, 13

This is the one that I am not sure quite sure about.
a2.m1();
a2.m2();
a2.m3(); = B's m1, A's m2, C's m3, 13

Thanks!
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, welcome to JavaRanch!

Have you tried compiling this code and seeing what the output is? If any output differs from what you expect, we could help explain why.
 
John Dodge
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Hi John, welcome to JavaRanch!

Have you tried compiling this code and seeing what the output is? If any output differs from what you expect, we could help explain why.


I totally forgot I could have just done that to get my answer. All the output was what I expected. Thanks!
 
Sarvesh Allawadi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the answer provided for the last code candidate in the book (2nd edition) is incorrect. It's marked as B m1, C m2, A m3. whereas it is the one above it. B m1, A m2, C m3, 13.
That might have confused you.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What i dont understand is why i get the Class A void m1 result "A's m1, " when entering the following block:

a.m1(); <-- my question is about the output for this first method
b.m2();
c.m3();

I would assume that in Mixed2.java the m1 will be pointing at the Class B void m1 method since Class B is the override(extend) to Class A. So the first output would be Class B "B's m1, "

i appreciate any feedback.

regards,
Arend
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please check very carefully that the code shown in the first post is exactly the same as in the book. Method m1 is overridden only in B and m3 overridden only in C and m2 is not overridden. You can't get B's m2 printed anywhere, so calling m2 will always produce the output A's m2.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Ritchie,

now i understand.

i did not realize that the reference variable a decides that m1 in Class A will be used.

Regards,
Arend

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:thank you Ritchie
That's a pleasure
. . . now i understand.

i did not realize that the reference variable a decides that m1 in Class A will be used.

Regards,
Arend

I am afraid you don't appear to understand. It has nothing to do with the reference type, but the runtime type of the variable. You declare the variables as A which means they are allowed to have methods m1 m2 and m3. If class C declared a new method m4, it would not be possible to call it from an A reference. But it doesn't. As I said two of the methods are overridden (one in B and one in C). If the runtime type is C, you get the C version of the methods. That is how polymorphism works.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Richie, i went over it again and i understand it better now.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!