• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Derived Class function called twice...Why ?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry Can you explain it a bit more!!
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Yup, yup, yup. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic