Forums Register Login

Java - Overloading and Overriding of methods

+Pie Number of slices to send: Send
Below is the code
The output is 22.
What is the flow of this program ??
[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]
+Pie Number of slices to send: Send
moved to Java in General (beginning)
+Pie Number of slices to send: Send
FYI, this is the output I get:

I can't explain it; I'll be interested in seeing an answer too.
+Pie Number of slices to send: Send
Ahhh... I got it. First, a critical fact:
If the first statement in a derived class constructor is not a call to a base class constructor, the compiler inserts a call to the default base constructor for you.
Now, this is what happens:

  1. test.Main is invoked and prints "Before".
  2. new Extension() is executed. It calls Extension.Extension(), which does not call the base class constructor. The compiler therefore inserts a call to Base.Base().
  3. Base.Base() prints "Inside Base func."
  4. Base.Base() calls add(1), and since the object here is actually an instance of class Extension, Extension.add(1) is called.
  5. Extension.add(int) prints "Inside Extension:Add func 1", gives the member i of Base the value 2, and prints "Inside Extension func:Value of i 2". Control passed back to Base.Base().
  6. Base.Base() returns, passing control back to Extension.Extension().
  7. Extension.Extension() now prints "Inside Extension func" and calls add(2). The object here is an instance of class Extension, so Extension.add(2) is called.
  8. Extension.add(int) prints "Inside Extension:Add func 2", gives the member i of Base the value 2+(2*2)=6, and prints "Inside Extension func:Value of i 6". Control passed back to Extension.Extension().
  9. Extension.Extension() is now finished, and returns. Control passed back to test.main.
  10. The newly created Extension object is passed to test.bogo(Base), which calls add(8). Since the object is actually a member of class Extension, Extension.add(8) is called.
  11. Extension.add(int) prints "Inside Extension:Add func 8", gives the member i of Base the value 6+(8*2)=22, and prints "Inside Extension func:Value of i 22". Control passed back to test.bogo(Base).
  12. test.bogo(Base) then calls the print method for the object, which prints the current value of i for the object, 22.
  13. test.bogo is finished, and returns control to test.main.
  14. test.main is finished; the program exits.

  15. Wow...this was good, and I learned something handy. Thanks, Paras!
+Pie Number of slices to send: Send
Hi Roy,
That was a good explaination. Thanks!!.
But i still have a doubt.
When u r in the Base class constructor, when does it call Extension.add() and not Base.add() ?
Bye

Originally posted by Roy Tock:
Ahhh... I got it. First, a critical fact:
[b]If the first statement in a derived class constructor is not a call to a base class constructor, the compiler inserts a call to the default base constructor for you.

Now, this is what happens:


  1. test.Main is invoked and prints "Before".
  2. new Extension() is executed. It calls Extension.Extension(), which does not call the base class constructor. The compiler therefore inserts a call to Base.Base().
  3. Base.Base() prints "Inside Base func."
  4. Base.Base() calls add(1), and since the object here is actually an instance of class Extension, Extension.add(1) is called.
  5. Extension.add(int) prints "Inside Extension:Add func 1", gives the member i of Base the value 2, and prints "Inside Extension func:Value of i 2". Control passed back to Base.Base().
  6. Base.Base() returns, passing control back to Extension.Extension().
  7. Extension.Extension() now prints "Inside Extension func" and calls add(2). The object here is an instance of class Extension, so Extension.add(2) is called.
  8. Extension.add(int) prints "Inside Extension:Add func 2", gives the member i of Base the value 2+(2*2)=6, and prints "Inside Extension func:Value of i 6". Control passed back to Extension.Extension().
  9. Extension.Extension() is now finished, and returns. Control passed back to test.main.
  10. The newly created Extension object is passed to test.bogo(Base), which calls add(8). Since the object is actually a member of class Extension, Extension.add(8) is called.
  11. Extension.add(int) prints "Inside Extension:Add func 8", gives the member i of Base the value 6+(8*2)=22, and prints "Inside Extension func:Value of i 22". Control passed back to test.bogo(Base).
  12. test.bogo(Base) then calls the print method for the object, which prints the current value of i for the object, 22.
  13. test.bogo is finished, and returns control to test.main.
  14. test.main is finished; the program exits.

  15. Wow...this was good, and I learned something handy. Thanks, Paras![/B]


+Pie Number of slices to send: Send
I know this is too late to answer this post but this is for others looking for this topic.

First of all whenever a constructor is called than the first thing to be called is the constructor of the base class.
If the coder has implicitly provided a call to the constructor than the called constructor will be invoked.
Else the default constructor will be called.
Remember default construcor has no argument list.
So in this case the constructor for Base ith no argument will be called.

Second why add in Extension is called ?
this is because the object instance created is of Base but object reference passed is of Extension. So when a method will be called it will be searched in Extension class first because of its reference.
Even when you are in any function of Base but call to add(...) will call add(...) in Extension.
This is the game of object reference.
Hope it will help
If I am at any point wrong please correct me.
Bye
+Pie Number of slices to send: Send
 

Originally posted by Roy Tock:
Thanks, Paras!



Hmm. You give a fully-detailed answer to what was clearly a homework question (it's obviously not coming from any real project), to which the original poster had not bothered to give any thought at all, and you thank him?!

Others may disagree, but I think one should avoid giving answers to obvious homework questions, except when the poster shows that they have made a decent attempt at it first.

Remember, your or my company may hire this lazy person, on the strength of a qualification that they got from your work!

... unless your answer was subtly poisoned with wrongness, in which case respect
+Pie Number of slices to send: Send
 

Originally posted by Peter Chase:


Hmm. You give a fully-detailed answer to what was clearly a homework question (it's obviously not coming from any real project), to which the original poster had not bothered to give any thought at all, and you thank him?!

Others may disagree, but I think one should avoid giving answers to obvious homework questions, except when the poster shows that they have made a decent attempt at it first.

Remember, your or my company may hire this lazy person, on the strength of a qualification that they got from your work!

... unless your answer was subtly poisoned with wrongness, in which case respect





Agreed 100%....

This is not homework site, where you can find your homework...

You should show your work and then expect for some analysis....
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1088 times.
Similar Threads
Khalid Mock - Constructor Evaluation
Constructor Problem
Khalid question...
mughal question,need help
Java - Overloading and Overriding of methods
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:04:38.