Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Method Overriding

 
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the printing of the Output.
Am expecting the variables to print first and the method, But it turns out to be the other way round.
That is the method statement prints before the variable.
And I keeping asking myself why?
When I swaped how the printing is done I still get the same output.
Someone help me understand why?




 
Marshal
Posts: 69799
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Work out which order the methods are called in. You get as far as the first print statement and find the h field. Then what happens? Where is the value of h and what format is it stored in? Will the getH() call be executed before or after the + operator?

And always please tell us where such code comes from, so as to avoid copyright problems.
 
Campbell Ritchie
Marshal
Posts: 69799
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, do you know why you get 4 44 as the second line of output, rather than 44 44?
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recognize this, this is an Enthuware mock test question.

This has been discussed the following places by others.

Do any of the following posts help out?
  • https://coderanch.com/t/678147/certification/Enthuware
  • https://coderanch.com/t/658010/certification/Explain-reason-output-Enthuware
  • http://enthuware.com/forum/viewtopic.php?f=2&t=2075


  •  
    Kelvin Okornoe
    Ranch Hand
    Posts: 130
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    And always please tell us where such code comes from, so as to avoid copyright problems.



    The question comes from enthuware 1Z0 - 803 test 3 question 39.
     
    Kelvin Okornoe
    Ranch Hand
    Posts: 130
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Work out which order the methods are called in. You get as far as the first print statement and find the h field. Then what happens? Where is the value of h and what format is it stored in? Will the getH() call be executed before or after the + operator?




    This is how I understand what the code is printing.
    b.h will print 4, Because variables are invoked using the reference type than the Object at runtime, and b.getH() will invoke the Beta version of the method at runtime. When it gets to which variable is printed in class Beta. Beta's variable h gets printed and returned.
    Please correct me if am wrong!.

    Campbell Ritchie wrote:Will the getH() call be executed before or after the + operator?



    I don't know that. I guess that is what I what to know and understand.
    Why the method call gets printed before the variable is printed.
     
    Kelvin Okornoe
    Ranch Hand
    Posts: 130
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Pete Letkeman wrote:I recognize this, this is an Enthuware mock test question.

    This has been discussed the following places by others.

    Do any of the following posts help out?

  • https://coderanch.com/t/678147/certification/Enthuware
  • https://coderanch.com/t/658010/certification/Explain-reason-output-Enthuware
  • http://enthuware.com/forum/viewtopic.php?f=2&t=2075




  • Thank you Pete Letkeman, I think I will fall on it.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kelvin, please do not misunderstand my previous post. It was not an effort to stop the discussion about this by any means.
    I was hoping to point you to resources which may explain the mock question in ways that you may not have thought of before.
    Please continue with your discussion if needed.

    I think that Campbell Ritchie is really good with Java related questions and explaining things.
    He has helped me out in the past when I had Java questions/problems during my exam preparations, which I felt was fairly often.
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kelvin Okornoe wrote:Someone help me understand why?


    This code snippet (and its output) is already explained in great detail in this topic. So I'm pretty sure when you have carefully read this excellent topic you'll have a perfect understanding why this output is printed. If you still have doubts and/or questions, just let us know by replying to this topic.

    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kelvin Okornoe wrote:That is the method statement prints before the variable.
    And I keeping asking myself why?


    That's very well explained in this post. Definitely worth reading!
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kelvin Okornoe wrote:b.h will print 4, Because variables are invoked using the reference type than the Object at runtime, and b.getH() will invoke the Beta version of the method at runtime. When it gets to which variable is printed in class Beta. Beta's variable h gets printed and returned.
    Please correct me if am wrong!.


    Let's get really nitpicky b.h will print nothing (System.out.println will do the printing, not b.h)! b.h will evaluate to 4 (the reason why you've explained correctly). b.getH() will invoke the getH() method from Baap, but at runtime the getH() method from Beta is executed.

    Here are two very simple (and hopefully easy to remember) but very, very, very important rules (which you must know by heart):
  • Which instance variables you can access is determined at compile time based on the reference variable type.
  • Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism).


  • Here is a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code. So the compiler doesn't know and care) about the actual objects, the compiler only knows about the types of the reference variables.

    Hope it helps!
    Kind regards,
    Roel
     
    If you want to look young and thin, hang around old, fat people. Or this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic