my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Java - Overloading and Overriding of methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java - Overloading and Overriding of methods" Watch "Java - Overloading and Overriding of methods" New topic
Author

Java - Overloading and Overriding of methods

Paras Shah
Greenhorn

Joined: Jul 24, 2001
Posts: 3
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
moved to Java in General (beginning)


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Roy Tock
Ranch Hand

Joined: Jul 16, 2001
Posts: 83
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

Joined: Jul 16, 2001
Posts: 83
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

Joined: Jul 24, 2001
Posts: 3
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

Joined: Jul 26, 2006
Posts: 1
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

Joined: Oct 30, 2001
Posts: 1970
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


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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....


The Best way to predict your future is to create it - Every great individual common man
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java - Overloading and Overriding of methods