This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Unexpected output from simple code  RSS feed

 
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Code


Expected Output


Reasoning
  • Hidden field variable

  • Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. - Java Tutorials


  • Overridden Instance Method

  • An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method. - Java Tutorials



    Actual Output


    Can someone please explain this to me?
     
    Ranch Hand
    Posts: 57
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to be more specific. You added those print statements inside your getter methods which produce extra output. If you are confused about variable hiding and method overriding there is a quite good explanation on DZone
     
    Marshal
    Posts: 63781
    209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are correct about variable hiding, but you are wrong about execution order. When you call the getH() method, you get the overriding version in both cases because the runtime type of the object is Beta. What conusing names for the classes; if you had called them Superclass and Subclass, he problem would have been easier to understand. That means the class name is printed before the print statements in line 21 ans 23 can run. In both cases, since you are using the subclass' method, the sbcass' field is used. In line 21, however, the field is called on the declared type of the variable, so you get 4 printed, only not where you think it would be printed. In line 23, yuo are using the declared type of Beta, so the variable 44 is found.
    That tutorial correctly explains variable hiding, but it has a very confusing spelling error in, and it uses the misleading parent/child terminology which so many people seem to like.
     
    Eric Arnold
    Ranch Hand
    Posts: 47
    1
    IntelliJ IDE Java Slackware
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I found a good explanation written by the code authors here.
     
    You firghten me terribly. I would like to go home now. Here, take this tiny ad:
    global solutions you can do in your home or backyard
    https://coderanch.com/t/708587/global-solutions-home-backyard
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!