wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Derived Class function called twice...Why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Derived Class function called twice...Why ?" Watch "Derived Class function called twice...Why ?" New topic
Author

Derived Class function called twice...Why ?

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128


This code, when run, gives output :
doIt(10) in TopClass
doIt(10) in DerivedClass
doIt(100) in DerivedClass

Can any1 explain how and why it's calling the DerivedClass method doIt() twice with different parameters ?

Thanx


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Because the call to doIt in the TopClass constructor is polymorphic, that is, it will call the actual method on the object (which in the second case is an instance of DerivedClass).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

I'm sorry Can you explain it a bit more!!


If you are not laughing at yourself, then you just didn't get the joke.
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Originally posted by Ilja Preuss:
Because the call to doIt in the TopClass constructor is polymorphic, that is, it will call the actual method on the object...


That's understandable coz I called the TopClass object.
But what's not understandabe is this...


...which in the second case is an instance of DerivedClass.

I mean if I am making a derived class object, why is it calling the function doIt(10) in DerivedClass ?

Cud u elaborate ?

Thanx.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
Ilja, I'm a bit confused as well. I would have thought that it has to do with a constructor implicitly calling the superclass constructor. How is the doIt method polymorphic? It simply overrides TopClass.doIt.


Ping & DNS - my free Android networking tools app
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

When u create an instance of Topclass...there is only an object of Topclass..there is no derived class object here...and so the doit of topclass is executed...

But when u try ot create an instnace of derivedclass...

initially, the constructor of topclass is xcuted,this in turn calls doit(10) function ...but the doit(10) of topclass is not xcuted ...

The do it has been overridden in the child class and here the object that is calling the doit function is an instance of the derived class...so the

doit(10) of dervived class is xcuted...

and now when the doit(100) function is called in derived class ...

its a local call on a function of the same object.

Hope its clear...

Akumar.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sherry Jacob:

I mean if I am making a derived class object, why is it calling the function doIt(10) in DerivedClass ?

Cud u elaborate ?


Because that is simply what polymorphism means (and OOP basically is about): the actual method called for overriden methods is decided at runtime based on the actual type of the object.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Sherry]: I mean if I am making a derived class object, why is it calling the function doIt(10) in DerivedClass ?

Um, there are two parts to this. The "DerivedClass" part is there because, as Ilja said, all calls to doIt() are polymorphic, and the instance you're creating is really a DerivedClass instance, so that's the class whose implementation of doIt() is used. But the other part (maybe the first part) is: why is doIt(10) called at all when you make a DerivedClass instance? (Since the DerivedClass constructor shows only doIt(100).) The answer is that whenever you call the constructor of a subclass, a constructor of the superclass will be invoked first. (And if that superclass has a superclass of its own, that super-superclass constructor will also be invoked before anything else. Etc.) You can explicitly control this behavior using a super() constructor invocation (or a this() invocation). I'm going to skip escribing how these work - if you don't know about super() and this(), keep reading your intro Java text until they get to it. You can't really understand this point until you've covered that material. Basically if you do not include a super() or this() as the first line of a constructor, the compiler inserts one for you, assuming you meant to call the super() constructor with no arguments. Thus any class constructor will always call a constructor of its superclass, except for an Object() constructor which has no superclass. Thus in your code, calling

invoces the constructor

which, since it does not explicitly invoke super(), the compiler assumes it's equivalent to

The call to super() ends up calling the constructor of the parent class:

and that is why you see that doIt() is called (a second time) with an argument of 10.


"I'm not back." - Bill Harding, Twister
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

When the JVM sees the doIt() method in superclass, How does it call the Derived classes' doIt() method?? bcoz' at that point there is no Derived Class constructed at all.

A class can be constructed only when the call to the superclasses' constructor is finished.

Does that mean when the JVM encounters a doIt() method, it checks under what object construction it currently is and passses the method call to
that object?

Somebody Clarify!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Arun Kumarr:
When the JVM sees the doIt() method in superclass, How does it call the Derived classes' doIt() method?? bcoz' at that point there is no Derived Class constructed at all.


Well, the DerivedClass object is already there - it's just not fully initialized. So the TopClass constructor "knows" that it's currently working on a DerivedClass instance, and therefore calls the method defined there.

You are right, though, that the DerivedClass part of the object hasn't even begun to be initialized (which only effects it's fields, not the methods), which makes this technique kind of dangerous (the method could try to access uninitialized fields, which can cause hard to find bugs).
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Suppose if the doIt() methods are marked "static", The output is as follows,

doIt(10) in TopClass.
doIt(10) in TopClass.
doIt(20) in otherpackageDerivedClass.

Since the JVM decides which method to call on the fly, Why is that it is confused when the methods are static?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Arun Kumarr:

Since the JVM decides which method to call on the fly, Why is that it is confused when the methods are static?


Because that's only true for instance (non-static) methods. Static methods aren't polymorphic, as you've found out - they are fully decided about at compile time.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Thank you.
 
Consider Paul's rocket mass heater.
 
subject: Derived Class function called twice...Why ?
 
Similar Threads
override of private method
Inner Class Visibility
Inheritance Question
Shadowing Variable
How do u access the classes in interfaces ?