• 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

Overridden method invoked in the constructor. Please clear it for me.

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


The output is:

AAA
Bvar=0 ( I am confused over here.)
BBB
Bvar=2222
Avar=0

Why B.doSomething() is invoked in Class A's constructor? Please clear it for me.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bvar is initially initialized to zero but variable initializer is happen after super class object is created

I will give you sequence of execution

1 class loading
2 class initialization first super class and then sub class
3 static inialization or static variable initialization from super then sub class
4 then main of sub invokes
5 then call to super()due to new B()but Bvar initialized to default value
no instance variable initialization happens at this time
5 before A() runs all inastance of A are initialized
doSomething of B is called at that time
Bvar is having only default value of 0
6 then control go to B()
at this time Bvar is happen to 2222 before B() runs
7 in B() printing of BAvr which got value of 2222

I think you should know this execution process for understaing
then you got why Bvar = 0 in output of program

I hope this may help you
If I am wrong somewhere in explaination please notify me about that

regards
Ninad
 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ninad. I know the sequence of execution. Actually, what is confusing me is not the assignment of Bvar. I am confused why the doSomething() in the class B is invoked in the constructor of A.

My original answer to the question is,

AAA
A.doSomething() (The right answer is Bvar=0, I am confused)
BBB
Bvar=2222
Avar=0

You mentioned "doSomething of B is called at that time". Can you tell me why?

[ June 18, 2008: Message edited by: David L. Wei ]
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because doSomething() method is overriden in sub class B and we have instatiated class B
instance method selection is done at runtime in overriding
instance variable selection is done at compile time

A() constructor calls B's doSomthing() method because we have done new B()
not new A()
you can do new A() and see what happens
you may get
AAA
A.doSomthing()

constructor of A() select doSomething() method of class A not of class B

I hope this may help you
Regards
Ninad
reply
    Bookmark Topic Watch Topic
  • New Topic