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
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.
[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.
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?
Joined: Jul 11, 2001
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).