wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Dynamic polymorphism and memory allocation for object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dynamic polymorphism and memory allocation for object" Watch "Dynamic polymorphism and memory allocation for object" New topic
Author

Dynamic polymorphism and memory allocation for object

Rakesh Kumar Sahoo
Greenhorn

Joined: Nov 03, 2011
Posts: 1
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

Joined: Jul 14, 2010
Posts: 754

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


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14115
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7703
    
  20

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38486
    
  23
Only a couple of books? There are lots of books available with errors in, some a lot worse than that.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic polymorphism and memory allocation for object