• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Inheretance OCA 1Z0 -808

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi guys

i get the output as -1 but i am not able to figure out the logic. I mayb missing something. Can someone please explain

 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priyanka,

Attempting to see what's happening in the code you provided is not clear. I'm guessing the code was intentionally written to be confusing as some type of mind-bender. Well, that worked, it hurt my brain to be honest! There are many problems with the way it is written but rather than go into those details the fastest approach to unraveling the logic is to add a few print statements.



After the code is run, the output should look something like:



My suspicion is --id evaluates to -1 because at the time when the variable is used it hasn't been initialized.

I hope someone follows up with confirmation and elaboration because now I'm interested as well.
 
Marshal
Posts: 80099
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Gillette wrote:. . . the code was intentionally written to be confusing . . .

Too true. Cert exam revision books often use such confusing code. OP: please always tell us where such code comes from. Also please explain why there are line numbers n1, n2, etc.

There are many problems with the way it is written . . .

Too true. I can see three serious design problems:-
  • 1: Overridable (instance) method called from constructor.
  • 2: Method with same name as constructor.
  • 3: Superclass and subclass with fields with the same identifier (=name).
  • But as DG said, they like confusing code.

    Remember that field declarations are not polymorphic.


    My suspicion is --id evaluates to -1 because at the time when the variable is used it hasn't been initialized. . . .

    I am sure you are correct.
  • 1: Variable declared as Base: Base class loaded.
  • 2: Variable initialised as Derived.
  • 3: Derived constructor starts by calling its superclass' constructor.
  • 4: Base constructor calls Object constructor.
  • 5: Look up the order of initialisation in the Java® Language Specification (=JLS) to see when field initialisation occurs.
  • 6: Base() method called: this is polymorphic, so it calls the version in the runtime type, i.e. Derived.
  • 7: Base() method decrements and prints value of field in subclass object.
  • 8: Control returns to subclass object and initialisation of fields in subclass object occurs.
  • So the value of the field has its default value at the time the method runs. That was before field initialisation. Hence, voilà −1.
    Try changing the declaration to be final and see what happens. Find the section in the JLS describing the exact order of initialisation of variables when an object is constructed.
     
    My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic