• 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

Invoking methods from constructors

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

Answer : 40
Plz explain
[ July 26, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The addValue() method in Derived overrides the Base method; the method is resolved at runtime based on the object type. Therefore, the Derived addValue() method gets called twice (once for each constructor call).

When you post code, please post it between [ code ] and [ /code ] tags (remove the spaces first, though). It makes your code much easier to read.
[ July 26, 2005: Message edited by: Steve Morrow ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I really can�t understand why the result is 40 instead of 30.
When you do
<code>
Base b=new Derived();
</code>
, the Derived constructor wouldn�t be called after the Base one?, so value would be 0+10+20=30?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


When you instantiate the Derived class the addValue will be called on the Derived class.
So the output will be:
0+20+20 = 40

To get more clarity on the flow:
Put
System.out.println("In Base class addValue()");
and
System.out.println("In Derived class addValue()");
in the addValue methods of both the classes.
 
Damian Lopez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for the quick reply.
So to get it right, when you instantiate a sublass, the parent class constructor is called before, and if in that constructor there is a method call that is overriden in the subclass, the overriding one is called, not the overriden one.
Because of that there are 2 calls to the overriding method in this case, right?
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When you instatiate the Derived class ,the constructor of Base class gets invoked first.Since there is call to addValue() the value becomes value=0+10=10.Now constructor of Derived class gets invoked and there also a call to addValue() method.Now Value=value+20=10+20=30.

and when u call getValue is will give you 30 as result.
 
Damian Lopez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Amol, the result is 40 not 30, you are commiting the same mistake as i did when i first read the code. So, is it true what i said in my previous post?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Damian,

So to get it right, when you instantiate a sublass, the parent class constructor is called before, and if in that constructor there is a method call that is overriden in the subclass, the overriding one is called, not the overriden one.

Yes, you got that right. As suggested by Jaikiran, insert some print statements in both the constructors and addValue() to see how the program flows.



Joyce
 
Damian Lopez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it is clear now
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling an overridable method from a constructor

http://qa.jtiger.org/GetQAndA.action?qids=10
 
Damian Lopez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Tony, about this code:

The result is x=0, but why x has not been initialized with the value of 2?
If you add static to its declaration then its value is 2.
I dont understand how can you invoke a non static method from a class before creating an instance of it?!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a good question, I am trying to answer:

When system allocates memory in the heap for class Derived(when creating), it actually allocates space for instance variables too, which means variable x get allocated memory and declared(init to 0 by default), but, here is the catch: until the super construction finishs, the derived construtor won't execute initialization block for instance variables. So when you call m() from super class, the derived hasn't initialized instance variables yet, so x=0.

If I am wrong, please correct me.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add the code x = 10 in the m() method of the Toast class. Then you will know the order of the method call.



Now read what Steve Morrow said:


The addValue() method in Derived overrides the Base method; the method is resolved at runtime based on the object type. Therefore, the Derived addValue() method gets called twice (once for each constructor call)



Hope that helps. I might be wrong too.

Thanks,

-Vijay
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic