Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java - Overloading and Overriding of methods

 
Paras Shah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moved to Java in General (beginning)
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI, this is the output I get:

I can't explain it; I'll be interested in seeing an answer too.
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Paras Shah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]

 
Rahul KumarIndia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic