• 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

Doubt in Constructor invocation

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

From above code, the Output is
Derived value :null
Derived value :derived

But, why the showMethod() of Base class that is Line 1 is NOT invoked?
Please explain the execution flow of the program.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables are initialized after the super constructor returns. But because you invoke an overriden method in the super constructor the method uses variables that aren't initialized yet. That explains the null output.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your example shows exactly why it's dangerous to call methods that can be overridden from constructors.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am looking for the execution process of the program. Why the showDevice() of the super class, i.e. Base is NOT invoked. My expected output is
base value : base or null

Another doubt, The static variable/ class member is executed at the time of class loading instead of object creation.So, if this is correct, will be called before construct invocation.

Please help me to understand the concept.

FYI, Wouter, the answer is not clearing my doubts. Please go through the question again.
Thnaks
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijit Das wrote:Hi ,

I am looking for the execution process of the program. Why the showDevice() of the super class, i.e. Base is NOT invoked. My expected output is
base value : base or null


What is the actual type of the you are creating? The overridden methods are invoked on the actual object.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
According my understanding, the execution steps are as follows.

20 -> 21 -> 16 -> 17 -> 07 -> 08 -> 04 -> 05 -> ...........



Where number represents the line no. shown in the code block.

But, while debug, the execution steps are

20 -> 21 -> 16 -> 17 -> 07 -> 13 -> ->14 -> ....




I think, now it is cleared my question!!!

For Abimaran Kugathasan,

What is the actual type of the you are creating?



- Creating object of the type Derived

The overridden methods are invoked on the actual object.


- Not clear the question
Thanks
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your super class constructor, you've called the instance method. This method is called on the actual object of Derived type. So the the method in the Derived class is invoked.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime, yesterday, wrote:. . . it's dangerous to call methods that can be overridden from constructors

That's worth repeating.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic