• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic polymorphism and memory allocation for object

 
Rakesh Kumar Sahoo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question on dynamic polymorphism. When I do ParentClass ref = new DerivedClass(), parent class default constructor gets called. End of DerivedClass constructor memory gets allocated to derived class in Heap. Since ParentClass default constructor is also executed completely, does that mean in heap ParentClass object is also created ?

On executing this program, I get a reference of ParentClass pointing to Derived class object created in heap. Will there be object of parent class ?
Also how JVM decides to call the derived class method but prints the ParentClass "i" value.
Please explain in terms of instance variable and methods.

Thanks in advance.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rakesh Kumar Sahoo, welcome to the JavaRanch.

Could you edit your post and put your code wrapped by the code tags? It will be easier to help you.

thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

An object of a derived class is an object of its superclass, with the extra information for the derived class added on to it. So if you create an instance of class DerivedClass, then yes, an instance of ParentClass is also created - but this is part of the DerivedClass object, it's not a completely separate DerivedClass object.

The ParentClass part of the object will be initialized first, by calling one of the constructors of class ParentClass. After that's done, the DerivedClass part of the object will be initialized by calling one of the DerivedClass constructors.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh Kumar Sahoo wrote:I have a question on dynamic polymorphism....

Strictly speaking, "polymorphism" (at least the way most programmers use the term) is always dynamic, so there's no need to qualify it. Just to save you CTS later in life.

Also how JVM decides to call the derived class method but prints the ParentClass "i" value.

Because method calling, especially in the case of overriding, is generally a runtime decision. Unless you make a method final, the compiler can't make any determination of what the actual method to call will be, so it leaves it up to the JVM. In the case of the variable, it can make that decision, since you already told it that the variable 'ref' is a ParentClass. In fact, I doubt if the JVM even gets involved (except to do as it's told).

You override methods, not variables.

Winston
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Rakesh Kumar Sahoo wrote:I have a question on dynamic polymorphism....

Strictly speaking, "polymorphism" (at least the way most programmers use the term) is always dynamic, so there's no need to qualify it. Just to save you CTS later in life.


Sadly, there are a couple of fairly popular books out there that call overloaded methods "compile-time polymorphism". I hate this term, but it's not uncommon these days.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only a couple of books? There are lots of books available with errors in, some a lot worse than that.
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's not strictly wrong, but definitely confusing. In an object oriented language, polymorphism usually refers to different concrete types providing the same interface. In a functional language, it usually refers to the same function operating on different data-types, which can be considered overloading.

It would definitely be better to just stick to the jargon of the language though (as superfluous as that sounds).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic