• 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

Super class constructor calls derived class overridden method?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Here is a piece of code and its output. Could not understand why the derived class method is called:



When we run this, the output is:

In ctr of Test1()
In Test2.d()
In ctr of Test2()
2

My questions is -
Why did Test2.d() get called from the ctr of Test1()? And if it did, then why is the value of i printed as 2 and not 5?

Please reply asap.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d() is overridden in Test2. When the JVm executed the method "d()" on the object in the heap, it is an instance of Test2 that is there, so it is the method on Test2 that is executed.
That explanation may not be the best.

Change the code for Test2 to this:Does that help?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is an example of a potentially dangerous vulnerability. Any methods called from the constructor should be "private" or "final". Then that method could not be overridden, and that problem wouldn't occur.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing that I had forgotten was when "i" gets initialised. For some reason I thought that happened before the invocation of the A constructor (as to invoke that, via the implicit "super()", surely the constructor of B has been entered and to get into that, variable have initialsed?)

I quickly found out I was wrong though!
 
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
Variable initialization occurs between the call to super and any code you put in the constructor. That's why i is 2: it starts at 0, is set to 5 by the call to d(), then is set to 2 by the initializer.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob!
 
reply
    Bookmark Topic Watch Topic
  • New Topic