This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Creating an object and polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an object and polymorphism" Watch "Creating an object and polymorphism" New topic
Author

Creating an object and polymorphism

Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
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

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating an object and polymorphism
 
Similar Threads
Using the Keyword super
default constructors and constructors
Servlet Concepts
How Inheritance Affects Serialization
super class objects.