GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Invoking methods from constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Invoking methods from constructors" Watch "Invoking methods from constructors" New topic
Author

Invoking methods from constructors

kavin kumar
Greenhorn

Joined: Jul 21, 2005
Posts: 22

Answer : 40
Plz explain
[ July 26, 2005: Message edited by: Barry Gaunt ]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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 ]
Damian Lopez
Greenhorn

Joined: Jul 26, 2005
Posts: 17
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?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

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.


[My Blog] [JavaRanch Journal]
Damian Lopez
Greenhorn

Joined: Jul 26, 2005
Posts: 17
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?
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
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.


"There are no mistakes, only lessons"
Damian Lopez
Greenhorn

Joined: Jul 26, 2005
Posts: 17
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?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Jul 26, 2005
Posts: 17
Thanks, it is clear now
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Calling an overridable method from a constructor

http://qa.jtiger.org/GetQAndA.action?qids=10


Tony Morris
Java Q&A (FAQ, Trivia)
Damian Lopez
Greenhorn

Joined: Jul 26, 2005
Posts: 17
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?!
Byron Foo
Greenhorn

Joined: Jul 20, 2005
Posts: 4
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.
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
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
 
GeeCON Prague 2014
 
subject: Invoking methods from constructors