• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating an object and polymorphism

 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I've read in a book: "In Java a subclass collects automatically all visible properties of the superclass, but the objects in the hierarchy EXIST INDIVIDUALLY. This means: If a subclass is istantiated, the constructor automatically calls the constructor of the superclass to instantiate the upper object."
I knew about the constructor-chaining but never thought about the consequences. This means: When creating one single object (not of the superclass Object) there are really some more objects created? If so: Could anyone post me a link or give me a hint where to find more of this subject? I haven't found anything telling me what exactly happens during runtime if a child-object is created.

Another question: whether a method should be dynamically invoked or static, this is decided by the compiler, am I seeing this right?

Thanks in advance,

Sasha.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

This is a good question. According to the section Initializing the base class from Thinking in Java...
When you create an object of the derived class, it contains within it a subobject of the base class. This subobject is the same as if you had created an object of the base class by itself. It's just that from the outside, the subobject of the base class is wrapped within the derived-class object.

We would generally say there is one object created, because the "subobjects" are inseparable parts of the derived object. After all, whether we treat a reference to this object as the base type or the derived type, that reference still points to the same object.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case I am beginning to understand what really happens if an object inherits methods from a superclass.

Thanks for the answer and the link. Haven't heard about "Thinking in Java 3" yet. Seems to be an interesting ressource.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sasha Ruehmkorf:
...Haven't heard about "Thinking in Java 3" yet. Seems to be an interesting ressource.

The link is to the 3rd edition of Thinking in Java, which Bruce Eckel provided free online. However, starting with the 4th edition, Thinking in Java is not available as a free version.

So just be aware that the the online version is an older edition, and does not cover Java 5.0.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic