• 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 Mixed Message Chapter 7

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Richie, i went over it again and i understand it better now.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very new to this, but... how do you compile this part?


Where do we get "a2." from? We haven't declared it anywhere, have we?
 
Bagzhan Sadvakassov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bagzhan Sadvakassov wrote:I am very new to this, but... how do you compile this part?


Where do we get "a2." from? We haven't declared it anywhere, have we?



I am sorry I didn't notice last line of code. How can I erase my posts?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We only delete posts if there is something seriously wrong with them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic